-
-
Notifications
You must be signed in to change notification settings - Fork 600
Missing beforeResponse hook on some error responses #3254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you so much for spending time to make this info. In h3 v1 / nitro v2There is a PR to change the order of calling hooks For Nitro v2, (with h3 v1), I like your idea could instead trigger the necessary hook from within the error handler. Since Nitro 2.11 it is possible to stack multiple error handlers for this purpose. In h3 v2 / nitro v3
I think what could be best to investigate and possibly fix any issues before h3 v2 release is that we make sure |
Yeah I saw this PR, and understand that it may be risky at this point. Since this issue a result of the combination of this behavior plus nitro's error handler I thought it made sense to report here first.
Are you open to a PR for this change or do you mean this should be done within applications?
Yeah that makes sense. Maybe we could also clarify the expected behavior of the error hook? My original assumption was that it was the equivalent to The other question I had is should the error hook also be called for something like |
Environment
Nitropack@2.11.7 h3@1.15.1
Reproduction
https://stackblitz.com/edit/unjs-nitro-starter-ppkblahg?file=server%2Froutes%2Fthrow.ts
Describe the bug
Depending on how an error is caused, the
beforeResponse
hook may not be called.Here are a few examples from the reproduction
return new Error()
throw new Error()
new Response(null, { status: 503 })
I would expect the
beforeResponse
hook to be called even for missing routes or thrown errors to allow intercepting those responses.Digging through the code, it looks like the root cause may be that nitro's
onError
handler actually sends the response https://github.com/nitrojs/nitro/blob/v2/src/runtime/error.ts#L82This marks the event as handled, so h3 will not call the
beforeResponse
/afterResponse
hooks https://github.com/unjs/h3/blob/v1/src/adapters/node.ts#L73Maybe a solution could be to also trigger those hooks within the error handler, before and after the
send
calls.Additional context
Follow up from #2560
Logs
The text was updated successfully, but these errors were encountered: