Add http error logging

pull/4/head
maru 8 months ago
parent dcb1c76370
commit 0eed542b2e
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -35,13 +35,13 @@ type AccountInfoResponse struct {
func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) { func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) {
username, err := getUsernameFromRequest(r) username, err := getUsernameFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
uuid, err := getUuidFromRequest(r) // lazy uuid, err := getUuidFromRequest(r) // lazy
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -66,7 +66,7 @@ func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal(AccountInfoResponse{Username: username, LastSessionSlot: latestSaveId}) response, err := json.Marshal(AccountInfoResponse{Username: username, LastSessionSlot: latestSaveId})
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -80,17 +80,17 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) {
var request AccountRegisterRequest var request AccountRegisterRequest
err := json.NewDecoder(r.Body).Decode(&request) err := json.NewDecoder(r.Body).Decode(&request)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
if !isValidUsername(request.Username) { if !isValidUsername(request.Username) {
http.Error(w, "invalid username", http.StatusBadRequest) httpError(w, r, "invalid username", http.StatusBadRequest)
return return
} }
if len(request.Password) < 6 { if len(request.Password) < 6 {
http.Error(w, "invalid password", http.StatusBadRequest) httpError(w, r, "invalid password", http.StatusBadRequest)
return return
} }
@ -98,7 +98,7 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) {
_, err = rand.Read(uuid) _, err = rand.Read(uuid)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to generate uuid: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to generate uuid: %s", err), http.StatusInternalServerError)
return return
} }
@ -106,13 +106,13 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) {
_, err = rand.Read(salt) _, err = rand.Read(salt)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to generate salt: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to generate salt: %s", err), http.StatusInternalServerError)
return return
} }
err = db.AddAccountRecord(uuid, request.Username, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength), salt) err = db.AddAccountRecord(uuid, request.Username, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength), salt)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to add account record: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to add account record: %s", err), http.StatusInternalServerError)
return return
} }
@ -127,33 +127,33 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) {
var request AccountLoginRequest var request AccountLoginRequest
err := json.NewDecoder(r.Body).Decode(&request) err := json.NewDecoder(r.Body).Decode(&request)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
if !isValidUsername(request.Username) { if !isValidUsername(request.Username) {
http.Error(w, "invalid username", http.StatusBadRequest) httpError(w, r, "invalid username", http.StatusBadRequest)
return return
} }
if len(request.Password) < 6 { if len(request.Password) < 6 {
http.Error(w, "invalid password", http.StatusBadRequest) httpError(w, r, "invalid password", http.StatusBadRequest)
return return
} }
key, salt, err := db.FetchAccountKeySaltFromUsername(request.Username) key, salt, err := db.FetchAccountKeySaltFromUsername(request.Username)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
http.Error(w, "account doesn't exist", http.StatusBadRequest) httpError(w, r, "account doesn't exist", http.StatusBadRequest)
return return
} }
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, r, err.Error(), http.StatusInternalServerError)
return return
} }
if !bytes.Equal(key, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength)) { if !bytes.Equal(key, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength)) {
http.Error(w, "password doesn't match", http.StatusBadRequest) httpError(w, r, "password doesn't match", http.StatusBadRequest)
return return
} }
@ -161,19 +161,19 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) {
_, err = rand.Read(token) _, err = rand.Read(token)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to generate token: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to generate token: %s", err), http.StatusInternalServerError)
return return
} }
err = db.AddAccountSession(request.Username, token) err = db.AddAccountSession(request.Username, token)
if err != nil { if err != nil {
http.Error(w, "failed to add account session", http.StatusInternalServerError) httpError(w, r, "failed to add account session", http.StatusInternalServerError)
return return
} }
response, err := json.Marshal(AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)}) response, err := json.Marshal(AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)})
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -184,23 +184,23 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleAccountLogout(w http.ResponseWriter, r *http.Request) { func (s *Server) handleAccountLogout(w http.ResponseWriter, r *http.Request) {
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization")) token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest)
return return
} }
if len(token) != 32 { if len(token) != 32 {
http.Error(w, "invalid token", http.StatusBadRequest) httpError(w, r, "invalid token", http.StatusBadRequest)
return return
} }
err = db.RemoveSessionFromToken(token) err = db.RemoveSessionFromToken(token)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
http.Error(w, "token not found", http.StatusBadRequest) httpError(w, r, "token not found", http.StatusBadRequest)
return return
} }
http.Error(w, "failed to remove account session", http.StatusInternalServerError) httpError(w, r, "failed to remove account session", http.StatusInternalServerError)
return return
} }

@ -81,7 +81,7 @@ func (s *Server) handleSeed(w http.ResponseWriter) {
func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) { func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) {
uuid, err := getUuidFromRequest(r) uuid, err := getUuidFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -94,7 +94,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Has("category") { if r.URL.Query().Has("category") {
category, err = strconv.Atoi(r.URL.Query().Get("category")) category, err = strconv.Atoi(r.URL.Query().Get("category"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
return return
} }
} }
@ -103,7 +103,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Has("page") { if r.URL.Query().Has("page") {
page, err = strconv.Atoi(r.URL.Query().Get("page")) page, err = strconv.Atoi(r.URL.Query().Get("page"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest)
return return
} }
} }
@ -115,7 +115,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal(rankings) response, err := json.Marshal(rankings)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -130,7 +130,7 @@ func (s *Server) handleRankingPageCount(w http.ResponseWriter, r *http.Request)
if r.URL.Query().Has("category") { if r.URL.Query().Has("category") {
category, err = strconv.Atoi(r.URL.Query().Get("category")) category, err = strconv.Atoi(r.URL.Query().Get("category"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
return return
} }
} }
@ -142,7 +142,7 @@ func (s *Server) handleRankingPageCount(w http.ResponseWriter, r *http.Request)
response, err := json.Marshal(pageCount) response, err := json.Marshal(pageCount)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }

@ -0,0 +1,11 @@
package api
import (
"log"
"net/http"
)
func httpError(w http.ResponseWriter, r *http.Request, error string, code int) {
log.Printf("%s: %s %d\n", r.URL.Path, error, code)
http.Error(w, error, code)
}

@ -41,10 +41,10 @@ func updateStats() {
} }
// /game/playercount - get player count // /game/playercount - get player count
func (s *Server) handlePlayerCountGet(w http.ResponseWriter) { func (s *Server) handlePlayerCountGet(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal(playerCount) response, err := json.Marshal(playerCount)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -52,14 +52,14 @@ func (s *Server) handlePlayerCountGet(w http.ResponseWriter) {
} }
// /game/titlestats - get title stats // /game/titlestats - get title stats
func (s *Server) handleTitleStatsGet(w http.ResponseWriter) { func (s *Server) handleTitleStatsGet(w http.ResponseWriter, r *http.Request) {
titleStats := &defs.TitleStats{ titleStats := &defs.TitleStats{
PlayerCount: playerCount, PlayerCount: playerCount,
BattleCount: battleCount, BattleCount: battleCount,
} }
response, err := json.Marshal(titleStats) response, err := json.Marshal(titleStats)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -67,10 +67,10 @@ func (s *Server) handleTitleStatsGet(w http.ResponseWriter) {
} }
// /game/classicsessioncount - get classic session count // /game/classicsessioncount - get classic session count
func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter) { func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal(classicSessionCount) response, err := json.Marshal(classicSessionCount)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }

@ -35,11 +35,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handleAccountLogout(w, r) s.handleAccountLogout(w, r)
case "/game/playercount": case "/game/playercount":
s.handlePlayerCountGet(w) s.handlePlayerCountGet(w, r)
case "/game/titlestats": case "/game/titlestats":
s.handleTitleStatsGet(w) s.handleTitleStatsGet(w, r)
case "/game/classicsessioncount": case "/game/classicsessioncount":
s.handleClassicSessionCountGet(w) s.handleClassicSessionCountGet(w, r)
case "/savedata/get": case "/savedata/get":
s.handleSavedataGet(w, r) s.handleSavedataGet(w, r)

@ -22,7 +22,7 @@ const sessionSlotCount = 3
func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
uuid, err := getUuidFromRequest(r) uuid, err := getUuidFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -30,13 +30,13 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
case "0": // System case "0": // System
system, err := readSystemSaveData(uuid) system, err := readSystemSaveData(uuid)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, r, err.Error(), http.StatusInternalServerError)
return return
} }
saveJson, err := json.Marshal(system) saveJson, err := json.Marshal(system)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
return return
} }
@ -44,30 +44,30 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
case "1": // Session case "1": // Session
slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return return
} }
if slotId < 0 || slotId >= sessionSlotCount { if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return return
} }
session, err := readSessionSaveData(uuid, slotId) session, err := readSessionSaveData(uuid, slotId)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) httpError(w, r, err.Error(), http.StatusInternalServerError)
return return
} }
saveJson, err := json.Marshal(session) saveJson, err := json.Marshal(session)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(saveJson) w.Write(saveJson)
default: default:
http.Error(w, "invalid data type", http.StatusBadRequest) httpError(w, r, "invalid data type", http.StatusBadRequest)
return return
} }
} }
@ -76,7 +76,7 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
uuid, err := getUuidFromRequest(r) uuid, err := getUuidFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -92,31 +92,31 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
var system defs.SystemSaveData var system defs.SystemSaveData
err = json.NewDecoder(r.Body).Decode(&system) err = json.NewDecoder(r.Body).Decode(&system)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
if system.TrainerId == 0 && system.SecretId == 0 { if system.TrainerId == 0 && system.SecretId == 0 {
http.Error(w, "invalid system data", http.StatusInternalServerError) httpError(w, r, "invalid system data", http.StatusInternalServerError)
return return
} }
err = db.UpdateAccountStats(uuid, system.GameStats) err = db.UpdateAccountStats(uuid, system.GameStats)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest)
return return
} }
var gobBuffer bytes.Buffer var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(system) err = gob.NewEncoder(&gobBuffer).Encode(system)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
return return
} }
zstdWriter, err := zstd.NewWriter(nil) zstdWriter, err := zstd.NewWriter(nil)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
return return
} }
@ -124,24 +124,24 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
err = os.MkdirAll("userdata/"+hexUuid, 0755) err = os.MkdirAll("userdata/"+hexUuid, 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
return return
} }
err = os.WriteFile("userdata/"+hexUuid+"/system.pzs", compressed, 0644) err = os.WriteFile("userdata/"+hexUuid+"/system.pzs", compressed, 0644)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
return return
} }
case "1": // Session case "1": // Session
slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return return
} }
if slotId < 0 || slotId >= sessionSlotCount { if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return return
} }
@ -153,20 +153,20 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
var session defs.SessionSaveData var session defs.SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session) err = json.NewDecoder(r.Body).Decode(&session)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
var gobBuffer bytes.Buffer var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(session) err = gob.NewEncoder(&gobBuffer).Encode(session)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
return return
} }
zstdWriter, err := zstd.NewWriter(nil) zstdWriter, err := zstd.NewWriter(nil)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
return return
} }
@ -174,17 +174,17 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
err = os.MkdirAll("userdata/"+hexUuid, 0755) err = os.MkdirAll("userdata/"+hexUuid, 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
return return
} }
err = os.WriteFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName), compressed, 0644) err = os.WriteFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName), compressed, 0644)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
return return
} }
default: default:
http.Error(w, "invalid data type", http.StatusBadRequest) httpError(w, r, "invalid data type", http.StatusBadRequest)
return return
} }
@ -195,7 +195,7 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) {
uuid, err := getUuidFromRequest(r) uuid, err := getUuidFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -210,18 +210,18 @@ func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) {
case "0": // System case "0": // System
err := os.Remove("userdata/" + hexUuid + "/system.pzs") err := os.Remove("userdata/" + hexUuid + "/system.pzs")
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
return return
} }
case "1": // Session case "1": // Session
slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return return
} }
if slotId < 0 || slotId >= sessionSlotCount { if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return return
} }
@ -232,11 +232,11 @@ func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) {
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName)) err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName))
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
return return
} }
default: default:
http.Error(w, "invalid data type", http.StatusBadRequest) httpError(w, r, "invalid data type", http.StatusBadRequest)
return return
} }
@ -251,7 +251,7 @@ type SavedataClearResponse struct {
func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) {
uuid, err := getUuidFromRequest(r) uuid, err := getUuidFromRequest(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) httpError(w, r, err.Error(), http.StatusBadRequest)
return return
} }
@ -262,19 +262,19 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) {
slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return return
} }
if slotId < 0 || slotId >= sessionSlotCount { if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return return
} }
var session defs.SessionSaveData var session defs.SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session) err = json.NewDecoder(r.Body).Decode(&session)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
@ -301,7 +301,7 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal(SavedataClearResponse{Success: newCompletion}) response, err := json.Marshal(SavedataClearResponse{Success: newCompletion})
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return
} }
@ -312,7 +312,7 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) {
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName)) err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
return return
} }

Loading…
Cancel
Save