go-chi / go-chi/chi

Middleware Chaining Not Respected by NotFound Handler

Open
#837 1 comment 0 reactions 0 assignees View on GitHub
routing
Dominant language
Go
Stars
22.8k
Forks
1.2k
Avg merge
5h 17m
Merged PRs (30d)
10

Description

I'm trying to set up a "catch all" route. Anything that goes to "catchall/some/other/thing" should go the `allHandler`. I have values that I am setting onto the context in my middleware. These values ONLY show up if I use `r.HandleFunc("/", allHandler )` - if I only use the `NotFound` handler, then the middleware is never set.
```
router := chi.NewRouter()
router.Route("/catchall", func(r chi.Router) {
r.Use(MiddlewareSetsContextValues)
r.NotFound(allHandler)
// uncomment the following so it works
// r.HandleFunc("/", allHandler) // wont match ../some/other/thing
})
```

This feels like a bug - I'd have expected the `r.HandleFunc` to not be needed to register the middleware.

Contributor guide

Open the contributing guide

Research direction

Reproduce the example starting with chi.NewRouter, Route, Use, NotFound, and HandleFunc, and observe whether middleware-set context values reach the NotFound handler. Trace the routing and middleware entry points involved; done means the catch-all NotFound handler receives the context values without requiring a separate HandleFunc route.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.