Skip to content

Throwing error in setErrorHandler after a JSON parse SyntaxError causes app to crash #5443

@RigoTamas

Description

@RigoTamas

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.26.2

Plugin version

No response

Node.js version

20.12.2

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

5.10.211-1-MANJARO

Description

Throwing any error in the setErrorHandler function after a JSON parse syntax error causes the app to crash.
Code to reproduce:

import fastify from 'fastify';

const main = async () => {
  const fastifyServer = fastify();
  fastifyServer.setErrorHandler((_error, _request, _reply) => {
    // Using `throw error` crashes the app as well.;
    throw new Error('something');
  });

  fastifyServer.post('/foo', (_req, res) => {
    res.status(200);
    res.send({ ok: true });
  });

  fastifyServer.listen({ port: 8000 });
};

void main();

Running the following request with a syntax error in the JSON body will crash the app:

curl --location --request POST 'localhost:8000/foo' \
--header 'Content-Type: application/json' \
--data-raw '{ "foo": "bar"'

Link to code that reproduces the bug

No response

Expected Behavior

Expected behavior would be that the thrown error is propogated to the root error handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions