Skip to content

fix: error throwing in reply#6299

Merged
Uzlopak merged 5 commits into
fastify:mainfrom
juanlet:bugfix/fix-error-throwing-in-reply.js
Sep 26, 2025
Merged

fix: error throwing in reply#6299
Uzlopak merged 5 commits into
fastify:mainfrom
juanlet:bugfix/fix-error-throwing-in-reply.js

Conversation

@juanlet

@juanlet juanlet commented Aug 24, 2025

Copy link
Copy Markdown
Contributor

Description

Instantiate FST_ERR_REP_READABLE_STREAM_LOCKED with new in sendWebStream, matching other thrown Fastify errors.

Checklist

  • run npm run test && npm run benchmark --if-present
  • tests are included (see test/reply-web-stream-locked.test.js)
  • documentation N/A
  • commit follows DCO and code of conduct

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

Tests seems not happy

@Eomm Eomm 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.

Checking the fastify/error docs, the fix seems legit!

Comment thread test/reply-web-stream-locked.test.js Outdated
@Eomm Eomm added the bugfix Issue or PR that should land as semver patch label Aug 24, 2025
@Eomm Eomm changed the title Bugfix/fix error throwing in reply fix: error throwing in reply Aug 24, 2025
Comment thread lib/reply.js

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

We are currently using a combination of both

Using new is a little faster with fastify-error as it internally checks if 'new' was used:

Node.js v22.18.0
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'new FST_ERR_HELLO_WORLD()''3060.9 ± 0.50%''3000.0 ± 41.00''331807 ± 0.06%''333333 ± 4619'        │ 32671   │
│ 1       │ 'FST_ERR_HELLO_WORLD()''3352.6 ± 0.41%''3292.0 ± 42.00''302058 ± 0.07%''303767 ± 3827'        │ 29828   │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'new FST_ERR_HELLO_WORLD()''3203.0 ± 0.32%''3083.0 ± 42.00''321557 ± 0.02%''324359 ± 4480'        │ 312208  │
│ 1       │ 'FST_ERR_HELLO_WORLD()''3349.8 ± 0.84%''3292.0 ± 42.00''302262 ± 0.01%''303767 ± 3926'        │ 298526  │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'new FST_ERR_HELLO_WORLD()''3051.4 ± 0.12%''3041.0 ± 42.00''329605 ± 0.01%''328839 ± 4494'        │ 327722  │
│ 1       │ 'FST_ERR_HELLO_WORLD()''3329.1 ± 0.13%''3292.0 ± 42.00''302065 ± 0.01%''303767 ± 3926'        │ 300383  │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'new FST_ERR_HELLO_WORLD()''3106.9 ± 0.19%''3041.0 ± 41.00''327357 ± 0.02%''328839 ± 4494'        │ 321861  │
│ 1       │ 'FST_ERR_HELLO_WORLD()''3350.2 ± 0.19%''3291.0 ± 41.00''302658 ± 0.02%''303859 ± 3833'        │ 298491  │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'FST_ERR_HELLO_WORLD()''3359.3 ± 0.15%''3292.0 ± 42.00''301922 ± 0.02%''303767 ± 3926'        │ 297679  │
│ 1       │ 'new FST_ERR_HELLO_WORLD()''3104.5 ± 0.64%''3041.0 ± 41.00''327729 ± 0.02%''328839 ± 4494'        │ 322115  │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
node test.mjs
Running benchmark...
┌─────────┬─────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                   │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'new FST_ERR_HELLO_WORLD()''3062.3 ± 0.14%''3000.0 ± 42.00''329698 ± 0.01%''333333 ± 4733'        │ 326554  │
│ 1       │ 'FST_ERR_HELLO_WORLD()''3325.5 ± 0.15%''3292.0 ± 42.00''302632 ± 0.01%''303767 ± 3926'        │ 300705  │
└─────────┴─────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
Benchmark
import { Bench } from 'tinybench';
import createError from '@fastify/error';

// --- Setup ---
// First, we create a custom error constructor using the factory.
// This is typically done once when your application starts.
const FST_ERR_HELLO_WORLD = createError(
  'FST_ERR_HELLO_WORLD',
  'Hello, %s!',
  500
);

const bench = new Bench({ time: 1000 });
let err = null;

// --- Benchmarking Scenarios ---

bench
  // Scenario 1: Using 'new' with the created error constructor.
  .add('new FST_ERR_HELLO_WORLD()', () => {
    err = new FST_ERR_HELLO_WORLD('world');
  })

  // Scenario 2: Calling the constructor directly as a function.
  .add('FST_ERR_HELLO_WORLD()', () => {
    err = FST_ERR_HELLO_WORLD('world');
  })

async function runBenchmark() {
  console.log('Running benchmark...');
  await bench.run();

  console.table(bench.table());
}

runBenchmark();

Please fix the test to use node:test

@gurgunday

Copy link
Copy Markdown
Member

I think we don't need the test here

@Uzlopak Uzlopak requested a review from gurgunday September 25, 2025 22:34
@Uzlopak Uzlopak merged commit 329e40f into fastify:main Sep 26, 2025
30 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

bugfix Issue or PR that should land as semver patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants