improved error page handling by tapping into Echo and its middleware

This commit is contained in:
2025-10-19 16:06:08 -06:00
parent e77ecf2a06
commit 13644f4ecb
5 changed files with 65 additions and 8 deletions
+41
View File
@@ -10,7 +10,12 @@
package main
import (
"fmt"
"net/http"
"git.erbosoft.com/amy/amsterdam/ui"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
)
/* NotImplPage is used for all TODO links, to show that something hasn't yet been implemented.
@@ -27,3 +32,39 @@ func NotImplPage(ctxt ui.AmContext) (string, any, error) {
ctxt.VarMap().Set("path", ctxt.URLPath())
return "framed_template", "notimpl.jet", nil
}
/* AmErrorHandler handles all the mundane HTTP errors generated by the Echo engine.
* Parameters:
* err - The error to be handled.
* c - The Echo context error is being handled on.
*/
func AmErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
return
}
errCode := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
errCode = he.Code
}
amctxt := ui.AmContextFromEchoContext(c)
if amctxt == nil {
var qerr error
amctxt, qerr = ui.AmCreateContext(c)
if qerr != nil {
log.Errorf("failed to create AmContext in error handler: %v", qerr)
c.String(errCode, fmt.Sprintf("error %d: %v", errCode, err))
return
}
}
amctxt.SetLeftMenu("top")
amctxt.SetRC(errCode)
amctxt.VarMap().Set("error", err.Error())
// TODO: come up with a way to shift templates and titles for different error codes
amctxt.VarMap().Set("amsterdam_pageTitle", "Amsterdam Internal Server Error")
cerr := ui.AmSendPageData(c, amctxt, "framed_template", "error.jet")
if cerr != nil {
log.Errorf("Error rendering error %d (%v): %v", errCode, err, cerr)
}
}