mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02: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{
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue