From 38c4b3f71e05590700185e8ec7ca7616e6a39fda Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Tue, 10 Feb 2026 18:04:36 -0700 Subject: [PATCH] factored out some common error messages to error objects that can be referenced --- community.go | 17 +++++++++++------ communityadmin.go | 2 +- conference.go | 4 ++-- conference_ops.go | 2 +- errors.go | 10 ++++++++++ invites.go | 4 ++-- login.go | 13 ++++++++----- userdata.go | 14 +++++++------- 8 files changed, 42 insertions(+), 24 deletions(-) diff --git a/community.go b/community.go index a497905..db9907b 100644 --- a/community.go +++ b/community.go @@ -19,8 +19,15 @@ import ( "git.erbosoft.com/amy/amsterdam/ui" "git.erbosoft.com/amy/amsterdam/util" "github.com/biter777/countries" + "github.com/labstack/echo/v4" ) +// ENOJOIN is an error for not being permitted to join a community. +var ENOJOIN *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to join this community") + +// ENOUNJOIN is an error for not being permitted to unjoin a community. +var ENOUNJOIN *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to unjoin this community") + /* ShowCommunity renders the community profile display. * Parameters: * ctxt - The AmContext for the request. @@ -163,7 +170,7 @@ func JoinCommunity(ctxt ui.AmContext) (string, any) { return "error", err } } else { - return "error", "you are not permitted to join this community" + return "error", ENOJOIN } return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) } @@ -236,8 +243,7 @@ func UnjoinCommunity(ctxt ui.AmContext) (string, any) { return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) } if lock { - ctxt.SetRC(http.StatusForbidden) - return "error", "you are not permitted to unjoin this community" + return "error", ENOUNJOIN } ctxt.VarMap().Set("comm", comm) ctxt.VarMap().Set("amsterdam_pageTitle", "Unjoin Community") @@ -263,8 +269,7 @@ func UnjoinCommunityConfirm(ctxt ui.AmContext) (string, any) { return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) } if lock { - ctxt.SetRC(http.StatusForbidden) - return "error", "you are not permitted to unjoin this community" + return "error", ENOUNJOIN } if ctxt.FormFieldIsSet("cancel") { return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) @@ -277,7 +282,7 @@ func UnjoinCommunityConfirm(ctxt ui.AmContext) (string, any) { ctxt.ClearCommunityContext() return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) } - return "error", "unknown button pressed to confirm unjoin" + return "error", EBUTTON } /* MemberList lists the members of the community. diff --git a/communityadmin.go b/communityadmin.go index da4d921..275bded 100644 --- a/communityadmin.go +++ b/communityadmin.go @@ -344,7 +344,7 @@ func EditCommunityLogo(ctxt ui.AmContext) (string, any) { happy = true return "redirect", "/comm/" + comm.Alias + "/admin/profile" } - return "error", "invalid button detected in logo upload" + return "error", EBUTTON } /* CreateCommunityForm renders the form for creating a new community. diff --git a/conference.go b/conference.go index b4c7236..f3652a7 100644 --- a/conference.go +++ b/conference.go @@ -293,7 +293,7 @@ func NewTopic(ctxt ui.AmContext) (string, any) { return "framed", "attachment_upload.jet" } - return "error", "invalid button clicked on form" + return "error", EBUTTON } /* breakRange breaks up a post range into two elements. @@ -710,7 +710,7 @@ func PostInTopic(ctxt ui.AmContext) (string, any) { } else if ctxt.FormFieldIsSet("posttopics") { returnURL = fmt.Sprintf("/comm/%s/conf/%s", comm.Alias, ctxt.GetScratch("currentAlias")) } else { - return "error", "unknown post button" + return "error", EBUTTON } // Check for slippage. diff --git a/conference_ops.go b/conference_ops.go index 68c760b..3dcb8a9 100644 --- a/conference_ops.go +++ b/conference_ops.go @@ -77,7 +77,7 @@ func AttachmentUpload(ctxt ui.AmContext) (string, any) { ctxt.VarMap().Set("errorMessage", err.Error()) return "framed", "attachment_upload.jet" } - return "error", "invalid button clicked on form" + return "error", EBUTTON } /* AttachmentSend sends the data of an attachment to the browser. diff --git a/errors.go b/errors.go index 49949f3..71eb2c6 100644 --- a/errors.go +++ b/errors.go @@ -10,6 +10,7 @@ package main import ( + "errors" "net/http" "git.erbosoft.com/amy/amsterdam/ui" @@ -17,12 +18,21 @@ import ( log "github.com/sirupsen/logrus" ) +// EBUTTON is the standard error for an unknown button. +var EBUTTON error = errors.New("invalid or unknown button pressed") + +// ELOGIN is the standard error for not being logged in +var ELOGIN error = errors.New("you are not logged in") + // ENOPERM is the standard "not permitted" error message. var ENOPERM *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to perform this operation") // ENOACCESS is the standard "no access" error message. var ENOACCESS *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to access this page") +// EPARAM is an error for no parameters being specified. +var EPARAM error = errors.New("no parameters specified") + /* NotImplPage is used for all TODO links, to show that something hasn't yet been implemented. * Parameters: * ctxt - The AmContext for the request. diff --git a/invites.go b/invites.go index dbaf297..ffde654 100644 --- a/invites.go +++ b/invites.go @@ -100,7 +100,7 @@ func InviteSend(ctxt ui.AmContext) (string, any) { if ctxt.FormFieldIsSet("cancel") { return "redirect", backlink } else if !ctxt.FormFieldIsSet("send") { - return "error", "invalid command" + return "error", EBUTTON } var comm *database.Community if ctxt.FormFieldIsSet("cid") { @@ -112,7 +112,7 @@ func InviteSend(ctxt ui.AmContext) (string, any) { return "error", err } } else { - return "error", "no parameters specified" + return "error", EPARAM } mode := "community" var conf *database.Conference = nil diff --git a/login.go b/login.go index 807525c..4d2fc88 100644 --- a/login.go +++ b/login.go @@ -21,6 +21,9 @@ import ( log "github.com/sirupsen/logrus" ) +// ENOACCOUNT is an error thrown if you have to log out before creating a new account. +var ENOACCOUNT error = errors.New("you cannot create a new account while logged in on an existing one. You must log out first") + /* LoginForm renders the Amsterdam login form. * Parameters: * ctxt - The AmContext for the request. @@ -170,7 +173,7 @@ func VerifyEmailForm(ctxt ui.AmContext) (string, any) { // If user is not logged in, this is an error. user := ctxt.CurrentUser() if user.IsAnon { - return "error", "you must log in before you can verify your account's E-mail address" + return "error", ELOGIN } // If user is already verified, this is a no-op. @@ -212,7 +215,7 @@ func VerifyEMail(ctxt ui.AmContext) (string, any) { // If user is not logged in, this is an error. user := ctxt.CurrentUser() if user.IsAnon { - return "error", "you must log in before you can verify your account's E-mail address" + return "error", ELOGIN } dlg, err := ui.AmLoadDialog("verify_email") @@ -279,7 +282,7 @@ func NewAccountUserAgreement(ctxt ui.AmContext) (string, any) { // If user is already logged in, this is an error. if !ctxt.CurrentUser().IsAnon { - return "error", "you cannot create a new account while logged in on an existing one. You must log out first" + return "error", ENOACCOUNT } ctxt.SetLeftMenu("top") @@ -305,7 +308,7 @@ func NewAccountForm(ctxt ui.AmContext) (string, any) { // If user is already logged in, this is an error. if !ctxt.CurrentUser().IsAnon { - return "error", "you cannot create a new account while logged in on an existing one. You must log out first" + return "error", ENOACCOUNT } dlg, err := ui.AmLoadDialog("newaccount") @@ -327,7 +330,7 @@ func NewAccountForm(ctxt ui.AmContext) (string, any) { func NewAccount(ctxt ui.AmContext) (string, any) { // If user is already logged in, this is an error. if !ctxt.CurrentUser().IsAnon { - return "error", "you cannot create a new account while logged in on an existing one. You must log out first" + return "error", ENOACCOUNT } dlg, err := ui.AmLoadDialog("newaccount") diff --git a/userdata.go b/userdata.go index 4dd4885..f9dd7b8 100644 --- a/userdata.go +++ b/userdata.go @@ -50,7 +50,7 @@ func EditProfileForm(ctxt ui.AmContext) (string, any) { } u := ctxt.CurrentUser() if u.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } dlg, err := ui.AmLoadDialog("profile") if err == nil { @@ -108,7 +108,7 @@ func EditProfileForm(ctxt ui.AmContext) (string, any) { func EditProfile(ctxt ui.AmContext) (string, any) { u := ctxt.CurrentUser() if u.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } dlg, err := ui.AmLoadDialog("profile") if err == nil { @@ -219,7 +219,7 @@ func ProfilePhotoForm(ctxt ui.AmContext) (string, any) { } u := ctxt.CurrentUser() if u.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } ci, err := u.ContactInfo(ctxt.Ctx()) if err == nil { @@ -242,7 +242,7 @@ func ProfilePhotoForm(ctxt ui.AmContext) (string, any) { func ProfilePhoto(ctxt ui.AmContext) (string, any) { u := ctxt.CurrentUser() if u.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } ci, err := u.ContactInfo(ctxt.Ctx()) if err != nil { @@ -313,7 +313,7 @@ func ProfilePhoto(ctxt ui.AmContext) (string, any) { happy = true return "redirect", "/profile?tgt=" + url.QueryEscape(target) } - return "error", "invalid button detected in photo upload" + return "error", EBUTTON } /* ShowProfile displays a user's profile. @@ -441,7 +441,7 @@ func ShowProfile(ctxt ui.AmContext) (string, any) { func QuickEMail(ctxt ui.AmContext) (string, any) { me := ctxt.CurrentUser() if me.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } myCI, err := me.ContactInfo(ctxt.Ctx()) if err != nil { @@ -481,7 +481,7 @@ func QuickEMail(ctxt ui.AmContext) (string, any) { func Hotlist(ctxt ui.AmContext) (string, any) { me := ctxt.CurrentUser() if me.IsAnon { - return "error", "you are not logged in" + return "error", ELOGIN } hotlist, err := database.AmGetConferenceHotlist(ctxt.Ctx(), me) if err != nil {