2024-06-09 09:31:30 -07:00
|
|
|
package handlers
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
|
2024-07-17 17:33:50 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
type Error struct {
|
2024-06-15 12:34:24 -07:00
|
|
|
*services.TemplateRenderer
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
func (e *Error) Page(err error, ctx echo.Context) {
|
2022-01-08 21:23:26 -08:00
|
|
|
if ctx.Response().Committed || context.IsCanceledError(err) {
|
2021-12-11 16:32:27 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
// Determine the error status code
|
2021-12-03 03:11:01 -08:00
|
|
|
code := http.StatusInternalServerError
|
|
|
|
if he, ok := err.(*echo.HTTPError); ok {
|
|
|
|
code = he.Code
|
|
|
|
}
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
// Log the error
|
2024-06-15 12:34:24 -07:00
|
|
|
logger := log.Ctx(ctx)
|
|
|
|
switch {
|
|
|
|
case code >= 500:
|
|
|
|
logger.Error(err.Error())
|
|
|
|
case code >= 400:
|
|
|
|
logger.Warn(err.Error())
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
// Render the error page
|
2024-06-15 12:34:24 -07:00
|
|
|
p := page.New(ctx)
|
|
|
|
p.Title = http.StatusText(code)
|
|
|
|
p.StatusCode = code
|
2024-07-27 12:28:45 -07:00
|
|
|
p.LayoutComponent = layouts.Main
|
2024-06-15 12:34:24 -07:00
|
|
|
p.HTMX.Request.Enabled = false
|
|
|
|
|
2024-07-17 17:33:50 -07:00
|
|
|
component := pages.Error(p)
|
|
|
|
|
|
|
|
if err = e.RenderPageTempl(ctx, p, component); err != nil {
|
2024-06-14 18:01:48 -07:00
|
|
|
log.Ctx(ctx).Error("failed to render error page",
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
}
|