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/ui"
"git.erbosoft.com/amy/amsterdam/util" "git.erbosoft.com/amy/amsterdam/util"
"github.com/biter777/countries" "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. /* ShowCommunity renders the community profile display.
* Parameters: * Parameters:
* ctxt - The AmContext for the request. * ctxt - The AmContext for the request.
@@ -163,7 +170,7 @@ func JoinCommunity(ctxt ui.AmContext) (string, any) {
return "error", err return "error", err
} }
} else { } else {
return "error", "you are not permitted to join this community" return "error", ENOJOIN
} }
return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) 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) return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias)
} }
if lock { if lock {
ctxt.SetRC(http.StatusForbidden) return "error", ENOUNJOIN
return "error", "you are not permitted to unjoin this community"
} }
ctxt.VarMap().Set("comm", comm) ctxt.VarMap().Set("comm", comm)
ctxt.VarMap().Set("amsterdam_pageTitle", "Unjoin Community") 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) return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias)
} }
if lock { if lock {
ctxt.SetRC(http.StatusForbidden) return "error", ENOUNJOIN
return "error", "you are not permitted to unjoin this community"
} }
if ctxt.FormFieldIsSet("cancel") { if ctxt.FormFieldIsSet("cancel") {
return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias)
@@ -277,7 +282,7 @@ func UnjoinCommunityConfirm(ctxt ui.AmContext) (string, any) {
ctxt.ClearCommunityContext() ctxt.ClearCommunityContext()
return "redirect", fmt.Sprintf("/comm/%s/profile", comm.Alias) 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. /* MemberList lists the members of the community.
+1 -1
View File
@@ -344,7 +344,7 @@ func EditCommunityLogo(ctxt ui.AmContext) (string, any) {
happy = true happy = true
return "redirect", "/comm/" + comm.Alias + "/admin/profile" 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. /* 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 "framed", "attachment_upload.jet"
} }
return "error", "invalid button clicked on form" return "error", EBUTTON
} }
/* breakRange breaks up a post range into two elements. /* 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") { } else if ctxt.FormFieldIsSet("posttopics") {
returnURL = fmt.Sprintf("/comm/%s/conf/%s", comm.Alias, ctxt.GetScratch("currentAlias")) returnURL = fmt.Sprintf("/comm/%s/conf/%s", comm.Alias, ctxt.GetScratch("currentAlias"))
} else { } else {
return "error", "unknown post button" return "error", EBUTTON
} }
// Check for slippage. // Check for slippage.
+1 -1
View File
@@ -77,7 +77,7 @@ func AttachmentUpload(ctxt ui.AmContext) (string, any) {
ctxt.VarMap().Set("errorMessage", err.Error()) ctxt.VarMap().Set("errorMessage", err.Error())
return "framed", "attachment_upload.jet" 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. /* AttachmentSend sends the data of an attachment to the browser.
+10
View File
@@ -10,6 +10,7 @@
package main package main
import ( import (
"errors"
"net/http" "net/http"
"git.erbosoft.com/amy/amsterdam/ui" "git.erbosoft.com/amy/amsterdam/ui"
@@ -17,12 +18,21 @@ import (
log "github.com/sirupsen/logrus" 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. // ENOPERM is the standard "not permitted" error message.
var ENOPERM *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to perform this operation") var ENOPERM *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to perform this operation")
// ENOACCESS is the standard "no access" error message. // ENOACCESS is the standard "no access" error message.
var ENOACCESS *echo.HTTPError = echo.NewHTTPError(http.StatusForbidden, "you are not permitted to access this page") 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. /* NotImplPage is used for all TODO links, to show that something hasn't yet been implemented.
* Parameters: * Parameters:
* ctxt - The AmContext for the request. * 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") { if ctxt.FormFieldIsSet("cancel") {
return "redirect", backlink return "redirect", backlink
} else if !ctxt.FormFieldIsSet("send") { } else if !ctxt.FormFieldIsSet("send") {
return "error", "invalid command" return "error", EBUTTON
} }
var comm *database.Community var comm *database.Community
if ctxt.FormFieldIsSet("cid") { if ctxt.FormFieldIsSet("cid") {
@@ -112,7 +112,7 @@ func InviteSend(ctxt ui.AmContext) (string, any) {
return "error", err return "error", err
} }
} else { } else {
return "error", "no parameters specified" return "error", EPARAM
} }
mode := "community" mode := "community"
var conf *database.Conference = nil var conf *database.Conference = nil
+8 -5
View File
@@ -21,6 +21,9 @@ import (
log "github.com/sirupsen/logrus" 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. /* LoginForm renders the Amsterdam login form.
* Parameters: * Parameters:
* ctxt - The AmContext for the request. * 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. // If user is not logged in, this is an error.
user := ctxt.CurrentUser() user := ctxt.CurrentUser()
if user.IsAnon { 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. // 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. // If user is not logged in, this is an error.
user := ctxt.CurrentUser() user := ctxt.CurrentUser()
if user.IsAnon { 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") 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 user is already logged in, this is an error.
if !ctxt.CurrentUser().IsAnon { 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") 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 user is already logged in, this is an error.
if !ctxt.CurrentUser().IsAnon { 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") dlg, err := ui.AmLoadDialog("newaccount")
@@ -327,7 +330,7 @@ func NewAccountForm(ctxt ui.AmContext) (string, any) {
func NewAccount(ctxt ui.AmContext) (string, any) { func NewAccount(ctxt ui.AmContext) (string, any) {
// If user is already logged in, this is an error. // If user is already logged in, this is an error.
if !ctxt.CurrentUser().IsAnon { 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") dlg, err := ui.AmLoadDialog("newaccount")
+7 -7
View File
@@ -50,7 +50,7 @@ func EditProfileForm(ctxt ui.AmContext) (string, any) {
} }
u := ctxt.CurrentUser() u := ctxt.CurrentUser()
if u.IsAnon { if u.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
dlg, err := ui.AmLoadDialog("profile") dlg, err := ui.AmLoadDialog("profile")
if err == nil { if err == nil {
@@ -108,7 +108,7 @@ func EditProfileForm(ctxt ui.AmContext) (string, any) {
func EditProfile(ctxt ui.AmContext) (string, any) { func EditProfile(ctxt ui.AmContext) (string, any) {
u := ctxt.CurrentUser() u := ctxt.CurrentUser()
if u.IsAnon { if u.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
dlg, err := ui.AmLoadDialog("profile") dlg, err := ui.AmLoadDialog("profile")
if err == nil { if err == nil {
@@ -219,7 +219,7 @@ func ProfilePhotoForm(ctxt ui.AmContext) (string, any) {
} }
u := ctxt.CurrentUser() u := ctxt.CurrentUser()
if u.IsAnon { if u.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
ci, err := u.ContactInfo(ctxt.Ctx()) ci, err := u.ContactInfo(ctxt.Ctx())
if err == nil { if err == nil {
@@ -242,7 +242,7 @@ func ProfilePhotoForm(ctxt ui.AmContext) (string, any) {
func ProfilePhoto(ctxt ui.AmContext) (string, any) { func ProfilePhoto(ctxt ui.AmContext) (string, any) {
u := ctxt.CurrentUser() u := ctxt.CurrentUser()
if u.IsAnon { if u.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
ci, err := u.ContactInfo(ctxt.Ctx()) ci, err := u.ContactInfo(ctxt.Ctx())
if err != nil { if err != nil {
@@ -313,7 +313,7 @@ func ProfilePhoto(ctxt ui.AmContext) (string, any) {
happy = true happy = true
return "redirect", "/profile?tgt=" + url.QueryEscape(target) return "redirect", "/profile?tgt=" + url.QueryEscape(target)
} }
return "error", "invalid button detected in photo upload" return "error", EBUTTON
} }
/* ShowProfile displays a user's profile. /* ShowProfile displays a user's profile.
@@ -441,7 +441,7 @@ func ShowProfile(ctxt ui.AmContext) (string, any) {
func QuickEMail(ctxt ui.AmContext) (string, any) { func QuickEMail(ctxt ui.AmContext) (string, any) {
me := ctxt.CurrentUser() me := ctxt.CurrentUser()
if me.IsAnon { if me.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
myCI, err := me.ContactInfo(ctxt.Ctx()) myCI, err := me.ContactInfo(ctxt.Ctx())
if err != nil { if err != nil {
@@ -481,7 +481,7 @@ func QuickEMail(ctxt ui.AmContext) (string, any) {
func Hotlist(ctxt ui.AmContext) (string, any) { func Hotlist(ctxt ui.AmContext) (string, any) {
me := ctxt.CurrentUser() me := ctxt.CurrentUser()
if me.IsAnon { if me.IsAnon {
return "error", "you are not logged in" return "error", ELOGIN
} }
hotlist, err := database.AmGetConferenceHotlist(ctxt.Ctx(), me) hotlist, err := database.AmGetConferenceHotlist(ctxt.Ctx(), me)
if err != nil { if err != nil {