Skip to content

Commit

Permalink
Add 5xx error handling (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jul 10, 2024
1 parent 7783f31 commit 0019389
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions presenter/access/html/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions presenter/access/html/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
13 changes: 13 additions & 0 deletions presenter/access/html/templates/5xx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>{{ .Message }}</title>
</head>
<body>
<h1>{{ .Code }} {{ .Message }}</h1>
<p>
Something went wrong.....
</p>
<hr>
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a>
</body>
</html>
13 changes: 13 additions & 0 deletions presenter/access/html/testdata/5xx.html.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>
Something went wrong.....
</p>
<hr>
Powered by <a href="https://github.com/teran/archived" target="_blank">archived</a>
</body>
</html>

0 comments on commit 0019389

Please sign in to comment.