Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add context to ErrorHandler #11

Open
pebo opened this issue Feb 9, 2024 · 5 comments · May be fixed by #13
Open

Add context to ErrorHandler #11

pebo opened this issue Feb 9, 2024 · 5 comments · May be fixed by #13

Comments

@pebo
Copy link

pebo commented Feb 9, 2024

In order to correlate custom ErrorHandler logging with requests if would be very helpful if the ErrorHandler signature was expanded to include context.Context e.g.

type ErrorHandler func(ctx context.Context, w http.ResponseWriter, message string, statusCode int)

This however is a breaking change so another option could be to expand Options with a ErrorHandlerWithContext field.

@pebo
Copy link
Author

pebo commented Feb 14, 2024

Added two example PRs for this.

Ran into the missing context when migration to chi from gin which has a context in the validation middleware:

type ErrorHandler func(c *gin.Context, message string, statusCode int)

@mikeschinkel
Copy link

There is definitely a need for more info in the error handler.

Context is one such need, but the current matched Route, the Request, and the Request's Headers — like Accept — are others.

I created a PR (#15) that (I think) addresses @pebo's use-case in this issue but also addresses others, and in a manner that will allow adding other info if such info is found to be needed by others in the future. Hopefully PR #15 can supersede PRs #13 and #14 since they change error handling but don't address the use-cases that #15 address.

@MattiasMartens
Copy link

Would love to see this merged! Seems it's waiting on a review from the maintainer.

@tmzane
Copy link

tmzane commented Dec 17, 2024

+1 for this feature.

I agree with @mikeschinkel that it should be not just the context.Context but the whole http.Request. It'd be also really useful to have access to the original error instead of just message string, to be able to match it with errors.Is/As. My use case is to ignore ErrPathNotFound/ErrMethodNotAllowed returned by the FindRounte method. Currently, we have to match the string, which is not perfect.

So I propose the new signature to be:

type ErrorHandler func(r *http.Request, w http.ResponseWriter, err error, statusCode int)

What do you guys think?

@mikeschinkel
Copy link

@tmzane — Definitely agree that having the original error could be useful. But changing the ErrorHandler signature would break existing code so I am not a fan of that.

Have you looked at PR #15? I could see it added to ErrorHandlerOpts if #15 is accepted to resolve this PR, #14, my issue (#15) and your issue with error.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants