Skip to content

setErrorHandler silently overrides previous handlers on a scoped instance #6096

@jean-michelet

Description

@jean-michelet

Prerequisites

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

Issue

Why does Fastify allow to override setErrorHandler without throwing an error?

This behavior can easily mislead users into thinking they can register multiple error handlers, or unintentionally override an existing one, leading to bugs:

  const app = fastify()

  app.setErrorHandler(() => {
    console.log('Free some resources...') // Never executed, memory leaks
  })
    .setErrorHandler(() => console.log('overridden'))

  app.get('/', () => { throw new Error('argh') })

  await app.inject({ url: '/' }) // Logs: "overridden"

Should I push a PR?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions