Skip to content

Commit

Permalink
refactoring: Use constants instead of literals (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran committed Aug 15, 2024
1 parent 2cf1d84 commit 663ee50
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions presenter/publisher/html/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/teran/archived/service"
)

const (
notFoundTemplateFilename = "404.html"
serverErrorTemplateFilename = "5xx.html"
)

type Handlers interface {
ContainerIndex(c echo.Context) error
VersionIndex(c echo.Context) error
Expand Down Expand Up @@ -72,7 +77,7 @@ func (h *handlers) VersionIndex(c echo.Context) error {
pagesCount, versions, err := h.svc.ListPublishedVersionsByPage(c.Request().Context(), container, page)
if err != nil {
if err == service.ErrNotFound {
return c.Render(http.StatusNotFound, "404.html", nil)
return c.Render(http.StatusNotFound, notFoundTemplateFilename, nil)
}
return err
}
Expand Down Expand Up @@ -113,7 +118,7 @@ func (h *handlers) ObjectIndex(c echo.Context) error {
pagesCount, objects, err := h.svc.ListObjectsByPage(c.Request().Context(), container, version, page)
if err != nil {
if err == service.ErrNotFound {
return c.Render(http.StatusNotFound, "404.html", nil)
return c.Render(http.StatusNotFound, notFoundTemplateFilename, nil)
}
return err
}
Expand Down Expand Up @@ -149,7 +154,7 @@ func (h *handlers) GetObject(c echo.Context) error {
url, err := h.svc.GetObjectURL(c.Request().Context(), container, version, key)
if err != nil {
if err == service.ErrNotFound {
return c.Render(http.StatusNotFound, "404.html", nil)
return c.Render(http.StatusNotFound, notFoundTemplateFilename, nil)
}
return err
}
Expand All @@ -159,7 +164,7 @@ func (h *handlers) GetObject(c echo.Context) error {

func (h *handlers) ErrorHandler(err error, c echo.Context) {
code := 500
templateFilename := "5xx.html"
templateFilename := serverErrorTemplateFilename

v, ok := err.(*echo.HTTPError)
if ok {
Expand All @@ -168,7 +173,7 @@ func (h *handlers) ErrorHandler(err error, c echo.Context) {
switch v.Code {
case http.StatusNotFound:
code = http.StatusNotFound
templateFilename = "404.html"
templateFilename = notFoundTemplateFilename
}
}

Expand Down

0 comments on commit 663ee50

Please sign in to comment.