mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 05:36:49 +00:00
simplify error handler
This commit is contained in:
parent
24799bd2db
commit
fc0f9c574b
1 changed files with 9 additions and 13 deletions
|
|
@ -77,27 +77,23 @@ func NewRouter(fs embed.FS) *echo.Echo {
|
||||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||||
Format: "${time_rfc3339} [${method}: ${status}] ${uri}; ip=${remote_ip}; ${latency_human}; ${user_agent}\n",
|
Format: "${time_rfc3339} [${method}: ${status}] ${uri}; ip=${remote_ip}; ${latency_human}; ${user_agent}\n",
|
||||||
}))
|
}))
|
||||||
e.HTTPErrorHandler = customHTTPErrorHandler(e)
|
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
||||||
// 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) {
|
|
||||||
code := http.StatusInternalServerError
|
code := http.StatusInternalServerError
|
||||||
var he *echo.HTTPError
|
var he *echo.HTTPError
|
||||||
if errors.As(err, &he) {
|
if errors.As(err, &he) {
|
||||||
code = he.Code
|
code = he.Code
|
||||||
}
|
}
|
||||||
|
|
||||||
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
||||||
if err := c.File(errorPage); err != nil {
|
if err := c.File(errorPage); err != nil {
|
||||||
c.Logger().Error(err)
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue