Skip to content

Commit

Permalink
feat: Non-existing pages now return styled 404 status page (with `4…
Browse files Browse the repository at this point in the history
…04` status code) (#189)

* 🎨 style(CHANGELOG.md): add UNRELEASED section
πŸ”§ chore(notfound/handler.go): refactor to use core.RespondWithErrorPage
πŸ”§ chore(http/server.go): pass config, templatePicker, renderer and options to notfoundHandler
πŸ”§ chore(hurl/404.hurl): update test to expect HTML response instead of plain text

* πŸ“ docs(CHANGELOG.md): fix typo in changed section

* πŸ”₯ chore(CHANGELOG.md): release version 2.22.0
πŸ”₯ chore(flags.go): remove unused code for serve command
  • Loading branch information
tarampampam authored Apr 7, 2023
1 parent 6a67510 commit d40e887
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 227 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## v2.22.0

### Changed

- Non-existing pages now return styled `404` status page (with `404` status code) [#188]

[#188]:https://github.com/tarampampam/error-pages/issues/188

## v2.21.0

### Changed
Expand Down
220 changes: 0 additions & 220 deletions internal/cli/serve/flags.go

This file was deleted.

22 changes: 18 additions & 4 deletions internal/http/handlers/notfound/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package notfound

import (
"github.com/valyala/fasthttp"

"gh.tarampamp.am/error-pages/internal/config"
"gh.tarampamp.am/error-pages/internal/http/core"
"gh.tarampamp.am/error-pages/internal/options"
"gh.tarampamp.am/error-pages/internal/tpl"
)

type (
templatePicker interface {
// Pick the template name for responding.
Pick() string
}

renderer interface {
Render(content []byte, props tpl.Properties) ([]byte, error)
}
)

// NewHandler creates handler missing requests handling.
func NewHandler() fasthttp.RequestHandler {
func NewHandler(cfg *config.Config, p templatePicker, rdr renderer, opt options.ErrorPage) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/plain; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusNotFound)
_, _ = ctx.WriteString("Wrong request URL. Error pages are available at the following URLs: /{code}.html")
core.RespondWithErrorPage(ctx, cfg, p, rdr, "404", fasthttp.StatusNotFound, opt)
}
}
2 changes: 1 addition & 1 deletion internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *Server) Register(cfg *config.Config, templatePicker templatePicker, opt

s.router.GET("/metrics", metricsHandler.NewHandler(reg))

s.router.NotFound = notfoundHandler.NewHandler()
s.router.NotFound = notfoundHandler.NewHandler(cfg, templatePicker, s.rdr, opt)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions test/hurl/404.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ GET http://{{ host }}:{{ port }}/not-found
HTTP/* 404

[Asserts]
header "Content-Type" contains "text/plain"
body contains "Wrong request URL"
header "Content-Type" contains "text/html"
body contains "The server can not find the requested page"

0 comments on commit d40e887

Please sign in to comment.