Files
amsterdam/errors.go
T

71 lines
2.1 KiB
Go

/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package main contains the high-level Amsterdam logic.
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.
* Parameters:
* ctxt - The AmContext for the request.
* Returns:
* Command string dictating what to be rendered.
* Data as a parameter for the command string.
* Standard Go error status.
*/
func NotImplPage(ctxt ui.AmContext) (string, any, error) {
ctxt.SetLeftMenu("top")
ctxt.VarMap().Set("amsterdam_pageTitle", "Function Not Implemented")
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)
}
}