Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@pkieltyka
Copy link
Member

Solves #9 and #29

@pkieltyka
Copy link
Member Author

This implementation is alright, but it would be nice if the NotFound() handler could cascade upwards back up the chain.

ie.

sub := chi.NewRouter()
sub.Get("/hello", ...)

root := chi.NewRouter()
root.Get("/", ...)
root.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  w.WriteHeader(404)
  w.Write([]byte("custom 404 page"))
})
root.Mount("/subrouter", sub)

a 404 on root render the custom error, but in the subrouter it will be the standard http.NotFound(), unless you define it again on that router.

This should be solvable. Options:

  1. Check end handler in handle() or Mux and apply the NotFound() method on the subrouter, but, we dont want to override it if someone set their own on the subrouter, so we'd have to check if its the default and thats not possible without another interface method that I'd rather not have.
  2. Go with a middleware.PageNotFound middleware as middlewares will go back up the chain naturally. This could work, and to trigger a 404, a handler would just return and the middleware would see no status was returned, so return a 404.
  3. Go with a package-level chi.NotFoundHandler = chi.HandlerFunc(func(ctx, w, r) { ... })
  4. Any other ideas?

@pkieltyka
Copy link
Member Author

This PR completes the NotFound() handler support, along with cascading up the tree looking for a not found handler.

pkieltyka pushed a commit that referenced this pull request Feb 26, 2016
NotFound() method on Router
@pkieltyka pkieltyka merged commit b9ba24f into master Feb 26, 2016
@pkieltyka pkieltyka deleted the issue-29 branch February 26, 2016 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants