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

fix(fastify): return the response on exception #10889

Merged

Conversation

jmcdo29
Copy link
Member

@jmcdo29 jmcdo29 commented Jan 16, 2023

Fastify requires us to return the response object when handling async methods and dealing with exceptions, otherwise a thenable gets wrapped and improperly handled later causing an exception. This is shown by this repo and discuessed in this issue.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

If we use a fastify plugin, like @fastify/compress and there is an error handled by the exception filter, we end up getting a response without any body, and if the onSend hook is debugged we will see that it gets called twice, once with the proper response and once with an undefined payload. Further debugging shows the message

{"level":30,"time":1673900898912,"pid":65238,"hostname":"nixos","reqId":"req-1","msg":"Response payload: undefined"}
{"level":50,"time":1673900898916,"pid":65238,"hostname":"nixos","reqId":"req-1","err":{"type":"Error","message":"premature close","stack":"Error: premature close\n    at onclosenexttick (~/node_modules/.pnpm/end-of-stream@1.4.4/node_modules/end-of-stream/index.js:54:86)\n    at processTicksAndRejections (node:internal/process/task_queues:77:11)"},"msg":"premature close"}

Issue Number: #10736

What is the new behavior?

Because we return the res object from the router-proxy now, we no longer get this issue with fastify trying to watch for the thenable and handling it internally.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I have verified that this does not affect express in any way.

Fastify requires us to return the response object when handling async
methods and dealing with exceptions, otherwise a thenable gets wrapped
and improperly handled later causing an exception. This is shown by
[this repo][1] and discuessed in [this issue][2].

[1]: https://github.com/sgrigorev/nestjs-compress-issue
[2]: fastify/fastify-compress#215 (comment)
@coveralls
Copy link

Pull Request Test Coverage Report for Build 8295f1dc-f16e-4be5-ac51-52a3a43184ac

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 93.411%

Totals Coverage Status
Change from base Build 43c8609d-7c9b-40c5-85e1-094c9c517952: 0.002%
Covered Lines: 6209
Relevant Lines: 6647

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants