diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-12 02:52:22 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-12 02:53:52 +0300 |
commit | d05ea66f4bbcf0cc5c8908f3435c68de1b070fa1 (patch) | |
tree | 7c7a769206646f2b81a0eda0680f0be5033a4197 /pkg/handler/error.go |
Начальная версияv0.0.1
Diffstat (limited to 'pkg/handler/error.go')
-rw-r--r-- | pkg/handler/error.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/pkg/handler/error.go b/pkg/handler/error.go new file mode 100644 index 0000000..eac791d --- /dev/null +++ b/pkg/handler/error.go @@ -0,0 +1,47 @@ +package handler + +import ( + "log" + "net/http" + + "github.com/labstack/echo/v4" + "go.neonxp.ru/framework/pkg/tpl" +) + +func ErrorHandler(err error, c echo.Context) { + if c.Response().Committed { + return + } + + he, ok := err.(*echo.HTTPError) + if ok { + if he.Internal != nil { + if herr, ok := he.Internal.(*echo.HTTPError); ok { + he = herr + } + } + } else { + he = &echo.HTTPError{ + Code: http.StatusInternalServerError, + Message: http.StatusText(http.StatusInternalServerError), + } + } + code := he.Code + message, ok := he.Message.(string) + if !ok { + message = "Неизвестная ошибка" + } + + if c.Request().Method == http.MethodHead { + err = c.NoContent(he.Code) + } else { + c.Response().WriteHeader(code) + if err := tpl.ErrorPage(code, message). + Render(c.Request().Context(), c.Response()); err != nil { + log.Println(err) + } + } + if err != nil { + log.Println(err) + } +} |