factored out some common error messages to error objects that can be referenced

This commit is contained in:
2026-02-10 18:04:36 -07:00
parent cf5d4422ac
commit 38c4b3f71e
8 changed files with 42 additions and 24 deletions
+11 -6
View File
@@ -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.
+1 -1
View File
@@ -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.
+2 -2
View File
@@ -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.
+1 -1
View File
@@ -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.
+10
View File
@@ -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.
+2 -2
View File
@@ -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
+8 -5
View File
@@ -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")
+7 -7
View File
@@ -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 {