nitro
nitro copied to clipboard
bug: manipulating cookies in the error hook gives the error `Cannot set headers after they are sent to the client`
Environment
Node.js: 18.20.3 Nitropack: 2.9.7
Reproduction
https://stackblitz.com/edit/github-dyj94s
Describe the bug
I created a plugin and set up an error hook, in the error hook I return a promise and use setTimeout to execute resolve, simulating the situation of awaiting a promise (possibly performing redis operations, etc).
However, it seems that when executing the error hook, the response headers were sent directly without waiting for the hook to finish executing, which resulted in a Cannot set headers after they are sent to the client
error in the setCookie in the setTimeout.
I don't know if this is intentional or if it's a hookable/h3 package error, but since I'm writing a session package, I need to handle session changes as well when the error occurs. Or am I using it incorrectly?
I will provide additional information if needed. Thank you.
Additional context
No response
Logs
handler false
promise false
timeout true
[nitro] [uncaughtException] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at __node_internal_captureLargerStackTrace2 (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:101:5335)
at new NodeError (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:101:4149)
at ServerResponse.setHeader (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:6:8796)
at Module.setCookie (file:///home/projects/github-dyj94s/node_modules/h3/dist/index.mjs:734:18)
at Timeout.eval [as _onTimeout] (file:///home/projects/github-dyj94s/.nitro/dev/index.mjs:693:74)
at https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:246:4692 {
code: 'ERR_HTTP_HEADERS_SENT'