Skip to content

docs: explain stream error handling#5746

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

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

Conversation

@lundibundi

Copy link
Copy Markdown
Contributor

Checklist

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

Copy link
Copy Markdown

+1 We have encountered this issue in production.

Comment thread docs/Reference/Server.md Outdated
@gurgunday

Copy link
Copy Markdown
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

Fdawgs commented Oct 17, 2024

Copy link
Copy Markdown
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?

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
Copy Markdown
Member

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

@Fdawgs Fdawgs left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

lundibundi commented Oct 21, 2024

Copy link
Copy Markdown
Contributor Author

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.

Comment thread docs/Reference/Server.md Outdated
Comment thread test/request-error.test.js Outdated
Comment thread test/request-error.test.js Outdated

@climba03003 climba03003 left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@lundibundi

Copy link
Copy Markdown
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.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tiny change

Comment thread docs/Reference/Server.md Outdated
climba03003
climba03003 previously approved these changes Jan 19, 2025
@jsumners jsumners requested a review from Fdawgs January 22, 2025 11:42
Comment thread docs/Reference/Server.md Outdated
Comment thread docs/Reference/Server.md Outdated
Co-authored-by: Frazer Smith <[email protected]>
Signed-off-by: Denys Otrishko <[email protected]>
@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.

@metcoder95 metcoder95 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Linting seems failing

@Fdawgs Fdawgs left a comment

Copy link
Copy Markdown
Member

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.

@Uzlopak

Uzlopak commented Sep 26, 2025

Copy link
Copy Markdown
Contributor

@metcoder95
@gurgunday
@climba03003

I fixed the tests to run properly. NEeds another approve

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

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 <[email protected]>

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 <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

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 <[email protected]>
Signed-off-by: James Sumners <[email protected]>

* Apply suggestions from code review

Co-authored-by: Frazer Smith <[email protected]>
Signed-off-by: Denys Otrishko <[email protected]>

* docs: explain stream error handling

* fix test

---------

Signed-off-by: James Sumners <[email protected]>
Signed-off-by: Denys Otrishko <[email protected]>
Signed-off-by: Aras Abbasi <[email protected]>
Co-authored-by: James Sumners <[email protected]>
Co-authored-by: Matteo Collina <[email protected]>
Co-authored-by: Frazer Smith <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

fix: error throwing in reply (fastify#6299)

* fix: instantiate readable stream error

* Create reply-web-stream-locked.test.js

Signed-off-by: Juan L. <[email protected]>
Signed-off-by: Juan Letamendia <[email protected]>

* fix test

---------

Signed-off-by: Juan L. <[email protected]>
Signed-off-by: Juan Letamendia <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

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 <[email protected]>

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 <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

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 <[email protected]>
Signed-off-by: James Sumners <[email protected]>

* Apply suggestions from code review

Co-authored-by: Frazer Smith <[email protected]>
Signed-off-by: Denys Otrishko <[email protected]>

* docs: explain stream error handling

* fix test

---------

Signed-off-by: James Sumners <[email protected]>
Signed-off-by: Denys Otrishko <[email protected]>
Signed-off-by: Aras Abbasi <[email protected]>
Co-authored-by: James Sumners <[email protected]>
Co-authored-by: Matteo Collina <[email protected]>
Co-authored-by: Frazer Smith <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

fix: error throwing in reply (fastify#6299)

* fix: instantiate readable stream error

* Create reply-web-stream-locked.test.js

Signed-off-by: Juan L. <[email protected]>
Signed-off-by: Juan Letamendia <[email protected]>

* fix test

---------

Signed-off-by: Juan L. <[email protected]>
Signed-off-by: Juan Letamendia <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>

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
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants