simplify error handler

This commit is contained in:
Ruidy 2024-02-29 18:08:20 +01:00
parent 24799bd2db
commit fc0f9c574b
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -77,27 +77,23 @@ func NewRouter(fs embed.FS) *echo.Echo {
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339} [${method}: ${status}] ${uri}; ip=${remote_ip}; ${latency_human}; ${user_agent}\n",
}))
e.HTTPErrorHandler = customHTTPErrorHandler(e)
// middlewares
e.Use(middleware.Recover())
e.Use(middleware.Secure())
// static assets
e.StaticFS("/static", echo.MustSubFS(fs, "assets"))
return e
}
func customHTTPErrorHandler(e *echo.Echo) echo.HTTPErrorHandler {
return func(err error, c echo.Context) {
e.HTTPErrorHandler = func(err error, c echo.Context) {
code := http.StatusInternalServerError
var he *echo.HTTPError
if errors.As(err, &he) {
code = he.Code
}
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
if err := c.File(errorPage); err != nil {
c.Logger().Error(err)
}
}
// middlewares
e.Use(middleware.Recover())
e.Use(middleware.Secure())
e.Use(middleware.Gzip())
// static assets
e.StaticFS("/static", echo.MustSubFS(fs, "assets"))
return e
}