Skip to content
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

bug: manipulating cookies in the error hook gives the error Cannot set headers after they are sent to the client #2771

Open
kiki-kanri opened this issue Oct 2, 2024 · 0 comments

Comments

@kiki-kanri
Copy link

kiki-kanri commented Oct 2, 2024

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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant