Switch from routes to self-registering handlers to group related routes.
This commit is contained in:
parent
30389de16f
commit
2c635b5c75
19 changed files with 719 additions and 717 deletions
42
pkg/handlers/error.go
Normal file
42
pkg/handlers/error.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
controller.Controller
|
||||
}
|
||||
|
||||
func (e *Error) Page(err error, ctx echo.Context) {
|
||||
if ctx.Response().Committed || context.IsCanceledError(err) {
|
||||
return
|
||||
}
|
||||
|
||||
code := http.StatusInternalServerError
|
||||
if he, ok := err.(*echo.HTTPError); ok {
|
||||
code = he.Code
|
||||
}
|
||||
|
||||
if code >= 500 {
|
||||
ctx.Logger().Error(err)
|
||||
} else {
|
||||
ctx.Logger().Info(err)
|
||||
}
|
||||
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageError
|
||||
page.Title = http.StatusText(code)
|
||||
page.StatusCode = code
|
||||
page.HTMX.Request.Enabled = false
|
||||
|
||||
if err = e.RenderPage(ctx, page); err != nil {
|
||||
ctx.Logger().Error(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue