From 001938912652978b8dbbcd9c1cdea53d4f461118 Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Wed, 10 Jul 2024 21:32:53 +0300 Subject: [PATCH] Add 5xx error handling (#37) Signed-off-by: Igor Shishkin --- presenter/access/html/handlers.go | 30 +++++++++++++++++++ presenter/access/html/handlers_test.go | 8 +++++ presenter/access/html/templates/5xx.html | 13 ++++++++ .../access/html/testdata/5xx.html.sample | 13 ++++++++ 4 files changed, 64 insertions(+) create mode 100644 presenter/access/html/templates/5xx.html create mode 100644 presenter/access/html/testdata/5xx.html.sample diff --git a/presenter/access/html/handlers.go b/presenter/access/html/handlers.go index 5b47dd3..3a2b339 100644 --- a/presenter/access/html/handlers.go +++ b/presenter/access/html/handlers.go @@ -99,11 +99,41 @@ func (h *handlers) GetObject(c echo.Context) error { return c.Redirect(http.StatusFound, url) } +func (h *handlers) ErrorHandler(err error, c echo.Context) { + code := 500 + templateFilename := "5xx.html" + + v, ok := err.(*echo.HTTPError) + if ok { + code = v.Code + + switch v.Code { + case http.StatusNotFound: + code = http.StatusNotFound + templateFilename = "404.html" + } + } + + type data struct { + Code int + Message string + } + + if err := c.Render(code, templateFilename, &data{ + Code: code, + Message: http.StatusText(code), + }); err != nil { + c.Logger().Error(err) + } +} + func (h *handlers) Register(e *echo.Echo) { e.Renderer = &renderer{ templates: template.Must(template.ParseGlob(path.Join(h.templateDir, "*.html"))), } + e.HTTPErrorHandler = h.ErrorHandler + e.GET("/", h.ContainerIndex) e.GET("/:container/", h.VersionIndex) e.GET("/:container/:version/", h.ObjectIndex) diff --git a/presenter/access/html/handlers_test.go b/presenter/access/html/handlers_test.go index 6240fdb..3573f63 100644 --- a/presenter/access/html/handlers_test.go +++ b/presenter/access/html/handlers_test.go @@ -59,6 +59,14 @@ func (s *handlersTestSuite) TestErrNotFound() { s.serviceMock.On("GetObjectURL", "test-container-1", "20240101010101", "test-object.txt").Return("", service.ErrNotFound).Once() s.compareHTMLResponse(s.srv.URL+"/test-container-1/20240101010101/test-object.txt", "testdata/404.html.sample") + + s.compareHTMLResponse(s.srv.URL+"/test-container-1/20240101010101", "testdata/404.html.sample") + s.compareHTMLResponse(s.srv.URL+"/test-container-1", "testdata/404.html.sample") +} + +func (s *handlersTestSuite) TestErr5xx() { + s.serviceMock.On("ListVersions", "test-container-1").Panic("blah").Once() + s.compareHTMLResponse(s.srv.URL+"/test-container-1/", "testdata/5xx.html.sample") } // Definitions ... diff --git a/presenter/access/html/templates/5xx.html b/presenter/access/html/templates/5xx.html new file mode 100644 index 0000000..3ee38b9 --- /dev/null +++ b/presenter/access/html/templates/5xx.html @@ -0,0 +1,13 @@ + + + {{ .Message }} + + +

{{ .Code }} {{ .Message }}

+

+ Something went wrong..... +

+
+ Powered by archived + + diff --git a/presenter/access/html/testdata/5xx.html.sample b/presenter/access/html/testdata/5xx.html.sample new file mode 100644 index 0000000..0e24ab3 --- /dev/null +++ b/presenter/access/html/testdata/5xx.html.sample @@ -0,0 +1,13 @@ + + + Internal Server Error + + +

500 Internal Server Error

+

+ Something went wrong..... +

+
+ Powered by archived + +