improved error page handling by tapping into Echo and its middleware
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user