Skip to content

docs: explain stream error handling#5746

Merged
Uzlopak merged 10 commits intofastify:mainfrom
lundibundi:docs-stream-error
Sep 26, 2025
Merged

docs: explain stream error handling#5746
Uzlopak merged 10 commits intofastify:mainfrom
lundibundi:docs-stream-error

Conversation

@lundibundi
Copy link
Contributor

Checklist

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Oct 17, 2024
@nechaido
Copy link

+1 We have encountered this issue in production.

@gurgunday
Copy link
Member

@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?

@Fdawgs
Copy link
Member

Fdawgs commented Oct 17, 2024

@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 docs/Guides but not for anything in docs/Reference.

@Fdawgs Fdawgs self-requested a review October 17, 2024 10:15
gurgunday
gurgunday previously approved these changes Oct 17, 2024
@climba03003
Copy link
Member

It is better to have some example showing this case. I may consider it as bug.

Copy link
Member

@Fdawgs Fdawgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking until I get a chance to properly review, can spot a few grammatical errors.

@lundibundi
Copy link
Contributor Author

Trying this out with fastify unit tests it looks like the part about if the error happens in the middle of the stream may not be correct - https://github.com/fastify/fastify/blob/main/lib/reply.js#L688-L693. I will update after further testing.

@lundibundi
Copy link
Contributor Author

lundibundi commented Oct 21, 2024

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).
Added few tests that check current behavior.

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 })
})

@lundibundi
Copy link
Contributor Author

@climba03003 thanks for the clarification, I'll update the documentation/test to mention explicit contentType instead of JSON.stringify.

mcollina
mcollina previously approved these changes Oct 25, 2024
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@lundibundi
Copy link
Contributor Author

@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.
Sorry for the delay.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny change

jsumners and others added 2 commits January 16, 2025 11:55
Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com>
climba03003
climba03003 previously approved these changes Jan 19, 2025
@jsumners jsumners requested a review from Fdawgs January 22, 2025 11:42
Co-authored-by: Frazer Smith <frazer.dev@icloud.com>
Signed-off-by: Denys Otrishko <shishugi@gmail.com>
@lundibundi lundibundi changed the base branch from main to 1.x January 28, 2025 12:18
@lundibundi lundibundi changed the base branch from 1.x to main January 28, 2025 12:18
@lundibundi lundibundi dismissed stale reviews from climba03003, mcollina, and gurgunday January 28, 2025 12:18

The base branch was changed.

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linting seems failing

Copy link
Member

@Fdawgs Fdawgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation-wise this is now good.

Signed-off-by: Aras Abbasi <aras.abbasi@googlemail.com>
@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 26, 2025

@metcoder95
@gurgunday
@climba03003

I fixed the tests to run properly. NEeds another approve

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Uzlopak Uzlopak merged commit 34fea68 into fastify:main Sep 26, 2025
32 checks passed
jean-michelet pushed a commit to jean-michelet/fastify that referenced this pull request Sep 27, 2025
… 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
jean-michelet pushed a commit to jean-michelet/fastify that referenced this pull request Sep 27, 2025
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants