Skip to content

Commit

Permalink
refactor SendError() function with proper error support
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 9, 2024
1 parent 4d6f6c7 commit 17b9e26
Show file tree
Hide file tree
Showing 35 changed files with 194 additions and 194 deletions.
6 changes: 3 additions & 3 deletions web/atm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *Api) GetATMBalance(w http.ResponseWriter, r *http.Request, claims *type

entry, err := a.app.DBContext.ModStorage.Get("atm", []byte(fmt.Sprintf("balance_%s", name)))
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -27,7 +27,7 @@ func (a *Api) GetATMBalance(w http.ResponseWriter, r *http.Request, claims *type
if entry != nil {
v, err := strconv.ParseInt(string(entry.Value), 10, 64)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
balance = int(v)
Expand All @@ -40,7 +40,7 @@ func (a *Api) ATMTransfer(w http.ResponseWriter, r *http.Request, claims *types.
req := &command.ATMTransferRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions web/cdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (a *Api) SearchCDBPackages(w http.ResponseWriter, r *http.Request, claims *
q := &cdb.PackageQuery{}
err := json.NewDecoder(r.Body).Decode(q)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down Expand Up @@ -52,7 +52,7 @@ func (a *Api) ResolveCDBPackageDependencies(w http.ResponseWriter, r *http.Reque
rr := &ResolveCDBPackageDepsRequest{}
err := json.NewDecoder(r.Body).Decode(rr)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
18 changes: 9 additions & 9 deletions web/change_pw.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *Api) ChangePassword(w http.ResponseWriter, r *http.Request, claims *typ
req := &ChangePasswordRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -27,18 +27,18 @@ func (a *Api) ChangePassword(w http.ResponseWriter, r *http.Request, claims *typ
// check username
if req.Username != claims.Username && !is_superuser {
// username does not match and "server" or "password" not found (not a superuser)
SendError(w, 500, "username mismatch and not a superuser")
SendError(w, 500, fmt.Errorf("username mismatch and not a superuser"))
return
}

// fetch entry from db
auth_entry, err := a.app.DBContext.Auth.GetByUsername(req.Username)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
if auth_entry == nil {
SendError(w, 404, "not found")
SendError(w, 404, fmt.Errorf("not found"))
return
}

Expand All @@ -51,17 +51,17 @@ func (a *Api) ChangePassword(w http.ResponseWriter, r *http.Request, claims *typ
// SRP fallback
salt, verifier, err := auth.ParseDBPassword(auth_entry.Password)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

ok, err := auth.VerifyAuth(req.Username, req.OldPassword, salt, verifier)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
if !ok {
SendError(w, 401, "unauthorized")
SendError(w, 401, fmt.Errorf("unauthorized"))
return
}
}
Expand All @@ -70,15 +70,15 @@ func (a *Api) ChangePassword(w http.ResponseWriter, r *http.Request, claims *typ
// create new password
salt, verifier, err := auth.CreateAuth(req.Username, req.NewPassword)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

// save to db
auth_entry.Password = auth.CreateDBPassword(salt, verifier)
err = a.app.DBContext.Auth.Update(auth_entry)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions web/chat_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func (a *Api) ExecuteChatcommand(w http.ResponseWriter, r *http.Request, claims
req := &command.ExecuteChatCommandRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

if req.Playername != claims.Username {
// username does not match
SendError(w, 500, "username mismatch")
SendError(w, 500, fmt.Errorf("username mismatch"))
return
}

Expand Down
6 changes: 3 additions & 3 deletions web/chat_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (a *Api) SendChat(w http.ResponseWriter, r *http.Request, c *types.Claims)
msg := &command.ChatMessage{}
err := json.NewDecoder(r.Body).Decode(msg)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
msg.Name = c.Username
Expand All @@ -42,13 +42,13 @@ func (a *Api) SendChat(w http.ResponseWriter, r *http.Request, c *types.Claims)
Message: msg.Message,
})
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

err = a.app.Bridge.SendCommand(command.COMMAND_CHAT_SEND, msg)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
26 changes: 13 additions & 13 deletions web/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (api *Api) Secure(fn SecureHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
claims, err := api.GetClaims(r)
if err == err_unauthorized {
SendError(w, http.StatusUnauthorized, "unauthorized")
SendError(w, http.StatusUnauthorized, fmt.Errorf("unauthorized"))
return
} else if err != nil {
api.RemoveClaims(w)
SendError(w, http.StatusInternalServerError, err.Error())
SendError(w, http.StatusInternalServerError, err)
return
}
fn(w, r, claims)
Expand All @@ -65,7 +65,7 @@ func (api *Api) OptionalSecure(fn SecureHandlerFunc) http.HandlerFunc {
return
} else if err != nil {
api.RemoveClaims(w)
SendError(w, http.StatusInternalServerError, err.Error())
SendError(w, http.StatusInternalServerError, err)
return
}
// logged in
Expand All @@ -79,15 +79,15 @@ func (api *Api) PrivCheck(required_priv string) Check {
return func(w http.ResponseWriter, r *http.Request) bool {
claims, err := api.GetClaims(r)
if err == err_unauthorized {
SendError(w, http.StatusUnauthorized, "unauthorized")
SendError(w, http.StatusUnauthorized, fmt.Errorf("unauthorized"))
return false
} else if err != nil {
api.RemoveClaims(w)
SendError(w, http.StatusInternalServerError, err.Error())
SendError(w, http.StatusInternalServerError, err)
return false
}
if !claims.HasPriv(required_priv) {
SendError(w, http.StatusForbidden, "forbidden, missing priv: "+required_priv)
SendError(w, http.StatusForbidden, fmt.Errorf("forbidden, missing priv: '%s'", required_priv))
return false
}
return true
Expand All @@ -98,12 +98,12 @@ func (api *Api) FeatureCheck(name string) Check {
return func(w http.ResponseWriter, r *http.Request) bool {
feature, err := api.app.Repos.FeatureRepository.GetByName(name)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return false
}

if !feature.Enabled {
SendError(w, http.StatusInternalServerError, fmt.Sprintf("Feature '%s' not enabled", name))
SendError(w, http.StatusInternalServerError, fmt.Errorf("Feature '%s' not enabled", name))
return false
}

Expand Down Expand Up @@ -161,14 +161,14 @@ func (api *Api) SecurePriv(required_priv string, fn SecureHandlerFunc) http.Hand
return func(w http.ResponseWriter, r *http.Request) {
claims, err := api.GetClaims(r)
if err == err_unauthorized {
SendError(w, http.StatusUnauthorized, "unauthorized")
SendError(w, http.StatusUnauthorized, fmt.Errorf("unauthorized"))
return
} else if err != nil {
SendError(w, http.StatusInternalServerError, err.Error())
SendError(w, http.StatusInternalServerError, err)
return
}
if !claims.HasPriv(required_priv) {
SendError(w, http.StatusForbidden, "forbidden, missing priv: "+required_priv)
SendError(w, http.StatusForbidden, fmt.Errorf("forbidden, missing priv: '%s'", required_priv))
return
}
fn(w, r, claims)
Expand All @@ -180,14 +180,14 @@ func (api *Api) Feature(name string, fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
feature, err := api.app.Repos.FeatureRepository.GetByName(name)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

if feature.Enabled {
fn(w, r)
} else {
SendError(w, http.StatusInternalServerError, fmt.Sprintf("Feature '%s' not enabled", name))
SendError(w, http.StatusInternalServerError, fmt.Errorf("Feature '%s' not enabled", name))
}
}
}
8 changes: 4 additions & 4 deletions web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (a *Api) GetConfig(w http.ResponseWriter, r *http.Request, c *types.Claims)

e, err := a.app.Repos.ConfigRepo.GetByKey(types.ConfigKey(key))
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -32,13 +32,13 @@ func (a *Api) SetConfig(w http.ResponseWriter, r *http.Request, c *types.Claims)
buf := bytes.NewBuffer([]byte{})
_, err := io.Copy(buf, r.Body)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

e, err := a.app.Repos.ConfigRepo.GetByKey(types.ConfigKey(key))
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -51,7 +51,7 @@ func (a *Api) SetConfig(w http.ResponseWriter, r *http.Request, c *types.Claims)
e.Value = buf.String()
err = a.app.Repos.ConfigRepo.Set(e)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion web/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (a *Api) SetControl(w http.ResponseWriter, r *http.Request, claims *types.C
req := &command.SetControlRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
6 changes: 3 additions & 3 deletions web/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (a *Api) GetFeatures(w http.ResponseWriter, r *http.Request) {

available_features, err := app.GetAvailableFeatures()
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
for _, avavailable_feature := range available_features {
Expand All @@ -22,7 +22,7 @@ func (a *Api) GetFeatures(w http.ResponseWriter, r *http.Request) {

list, err := a.app.Repos.FeatureRepository.GetAll()
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}
for _, feature := range list {
Expand All @@ -39,7 +39,7 @@ func (a *Api) SetFeature(w http.ResponseWriter, r *http.Request, claims *types.C
feature := &types.Feature{}
err := json.NewDecoder(r.Body).Decode(feature)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
12 changes: 6 additions & 6 deletions web/filebrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func (a *Api) get_sanitized_filename(r *http.Request, query_param string) (strin
func (a *Api) BrowseFolder(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
reldir, absdir, err := a.get_sanitized_dir(r)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

entries, err := os.ReadDir(absdir)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func (a *Api) BrowseFolder(w http.ResponseWriter, r *http.Request, claims *types
func (a *Api) Mkdir(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
reldir, absdir, err := a.get_sanitized_dir(r)
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -108,7 +108,7 @@ func (a *Api) Mkdir(w http.ResponseWriter, r *http.Request, claims *types.Claims
func (a *Api) DeleteFile(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
rel_filename, filename, err := a.get_sanitized_filename(r, "filename")
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand All @@ -125,13 +125,13 @@ func (a *Api) DeleteFile(w http.ResponseWriter, r *http.Request, claims *types.C
func (a *Api) RenameFile(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
rel_src, src, err := a.get_sanitized_filename(r, "src")
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

rel_dst, dst, err := a.get_sanitized_filename(r, "dst")
if err != nil {
SendError(w, 500, err.Error())
SendError(w, 500, err)
return
}

Expand Down
Loading

0 comments on commit 17b9e26

Please sign in to comment.