docs: explain stream error handling#5746
Conversation
|
+1 We have encountered this issue in production. |
|
@Fdawgs I lost the link that showed the best practices for writing docs, do you happen to remember where they were, so I can link it? |
https://fastify.dev/docs/latest/Guides/Style-Guide/ "You" is fine for any informal documentation in |
|
It is better to have some example showing this case. I may consider it as bug. |
Fdawgs
left a comment
There was a problem hiding this comment.
Blocking until I get a chance to properly review, can spot a few grammatical errors.
|
Trying this out with fastify unit tests it looks like the part about |
|
Removing the statement on stream/socket hangup as I was not able to reproduce this with Fastify tests. The logic in stream error handling looks sound so maybe something was wrong with the test setup (or jest or fastify.inject). |
There was a problem hiding this comment.
The whole test is based on the wrong concept of error handling.
When you explicitly set the Content-Type, we respect it even if it goes through the error handler. So, it does not serialize the object in this case.
You should either always explicit set the Content-Type in error handler,
import fastify from "fastify";
import { Readable } from "stream";
const app = fastify({ logger: true });
app.get('/', async function (request, reply) {
const stream = new Readable({
read() {
this.push('hello')
}
})
process.nextTick(() => stream.destroy(new Error('stream error')))
await reply.type('application/text').send(stream)
})
app.setErrorHandler((err, req, reply) => {
reply
.code(400)
.type('application/json')
.send({ error: err.message })
})
app.listen({ port: 3000 })or let fastify to guess the Content-Type in your route handler.
import fastify from "fastify";
import { Readable } from "stream";
const app = fastify({ logger: true });
app.get('/', async function (request, reply) {
const stream = new Readable({
read() {
this.push('hello')
}
})
process.nextTick(() => stream.destroy(new Error('stream error')))
await reply.send(stream)
})
app.setErrorHandler((err, req, reply) => {
reply.code(400)
reply.send({ error: err.message })
})|
@climba03003 thanks for the clarification, I'll update the documentation/test to mention explicit contentType instead of JSON.stringify. |
6db4dbe to
61aec9f
Compare
|
@climba03003 @Fdawgs @gurgunday @mcollina I've updated the docs and test cases according to the discussion as well as pulled most recent main branch, please take a look. |
Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com>
Co-authored-by: Frazer Smith <frazer.dev@icloud.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com>
The base branch was changed.
Fdawgs
left a comment
There was a problem hiding this comment.
Documentation-wise this is now good.
Signed-off-by: Aras Abbasi <aras.abbasi@googlemail.com>
|
@metcoder95 I fixed the tests to run properly. NEeds another approve |
… error-parameter for setErrorHandler and errorHandler to unknown, but configurable via generic TError (fastify#6308) * types: loosen setErrorHandler and errorHandler types * set TError to unknown add generic to errorHandler * fix * remove obsolte type tst * add unit test, fix potential issue --------- Co-authored-by: Jean <110341611+jean-michelet@users.noreply.github.com> build(deps-dev): remove @fastify/pre-commit (fastify#6319) `@fastify/pre-commit` isn't configured or used in this repo as there is no `pre-commit` object in package.json. It won't run regardless as scripts are disabled. Signed-off-by: Frazer Smith <frazer.dev@icloud.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> ci: improve citgm workflows (fastify#6334) * ci: improve citgm workflows * fix remark docs: explain stream error handling (fastify#5746) * docs: explain stream error handling * Update docs/Reference/Server.md Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Frazer Smith <frazer.dev@icloud.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com> * docs: explain stream error handling * fix test --------- Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com> Signed-off-by: Aras Abbasi <aras.abbasi@googlemail.com> Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Frazer Smith <frazer.dev@icloud.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> fix: error throwing in reply (fastify#6299) * fix: instantiate readable stream error * Create reply-web-stream-locked.test.js Signed-off-by: Juan L. <juanf03@gmail.com> Signed-off-by: Juan Letamendia <juanf03@gmail.com> * fix test --------- Signed-off-by: Juan L. <juanf03@gmail.com> Signed-off-by: Juan Letamendia <juanf03@gmail.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> docs: create 2 frst chapters of the tutorial docs: nit docs: create a fastify server docs: create basic routes docs: improve chapter 4 refactor: improve readability and quote examples docs: decoration chapter public part docs: decoration chapter public part fix: typo fix: typo fix: markdown lint docs: document internals docs: validatio nand serialization docs: move section mapping reminder with routes declarations docs: add hooks chapter docs: remove internals involved sections docs: add error handling chapter fix: copilot suggestions
… error-parameter for setErrorHandler and errorHandler to unknown, but configurable via generic TError (fastify#6308) * types: loosen setErrorHandler and errorHandler types * set TError to unknown add generic to errorHandler * fix * remove obsolte type tst * add unit test, fix potential issue --------- Co-authored-by: Jean <110341611+jean-michelet@users.noreply.github.com> build(deps-dev): remove @fastify/pre-commit (fastify#6319) `@fastify/pre-commit` isn't configured or used in this repo as there is no `pre-commit` object in package.json. It won't run regardless as scripts are disabled. Signed-off-by: Frazer Smith <frazer.dev@icloud.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> ci: improve citgm workflows (fastify#6334) * ci: improve citgm workflows * fix remark docs: explain stream error handling (fastify#5746) * docs: explain stream error handling * Update docs/Reference/Server.md Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Frazer Smith <frazer.dev@icloud.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com> * docs: explain stream error handling * fix test --------- Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com> Signed-off-by: Aras Abbasi <aras.abbasi@googlemail.com> Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Frazer Smith <frazer.dev@icloud.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> fix: error throwing in reply (fastify#6299) * fix: instantiate readable stream error * Create reply-web-stream-locked.test.js Signed-off-by: Juan L. <juanf03@gmail.com> Signed-off-by: Juan Letamendia <juanf03@gmail.com> * fix test --------- Signed-off-by: Juan L. <juanf03@gmail.com> Signed-off-by: Juan Letamendia <juanf03@gmail.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> docs: create 2 frst chapters of the tutorial docs: nit docs: create a fastify server docs: create basic routes docs: improve chapter 4 refactor: improve readability and quote examples docs: decoration chapter public part docs: decoration chapter public part fix: typo fix: typo fix: markdown lint docs: document internals docs: validatio nand serialization docs: move section mapping reminder with routes declarations docs: add hooks chapter docs: remove internals involved sections docs: add error handling chapter fix: copilot suggestions
Checklist
npm run testandnpm run benchmarkand the Code of conduct