Skip to content

Error handler has 200 as reply status code instead of 500 #4501

@HiiiiD

Description

@HiiiiD

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

Plugin version

No response

Node.js version

18.5.0

Operating system

Windows

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

10

Description

setErrorHandler(err,req,reply) returns 200 as reply.statusCode instead of 500.
I'd expect that if an API breaks due to unexpected error, the error handler reply status code should be 500.

This feature has changed from fastify 3 to fastify 4 (just tested it) because on Fastify 3 it correctly returns 500.

Steps to Reproduce

import fastify from 'fastify';

const app = fastify();

app.setErrorHandler((error, request, response) => {
    console.log(error, error.statusCode, response.statusCode);
    response.status(response.statusCode).send({ error: error.message })
})

app.get('/', (request, reply) => {
    const m = structuredClone(request);
})

await app.listen({ host: '0.0.0.0', port: 8080 })

Expected Behavior

Expected behavior is to have reply.statusCode equal to 500 instead of 200

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions