contrib/go-chi/chi: Handle 0 status code correctly #740
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the user doesn't call
w.WriteHeader(...)
orw.Write(...)
in the request handler or any of the middlewares the status isn't set andWrapResponseWriter.Status()
returns 0. This is well documented behavior: https://github.com/go-chi/chi/blob/cf97bef8b1405c9177bd88eca166ed4f16b9fc80/middleware/wrap_writer.go#L45In such a case
net/http
library takes the matters into its own hands and writes 200 status code: https://github.com/golang/go/blob/7e9369a517d9ebf867748719948d8cbccec3bc57/src/net/http/server.go#L1618Try this code:
with:
Result:
Observe the 000 status code logged by chi's logging middleware that similarly relies on
ww.Status()
https://github.com/go-chi/chi/blob/cf97bef8b1405c9177bd88eca166ed4f16b9fc80/middleware/logger.go#L43:Without changes proposed in this PR traces have empty status code in similar cases.