Skip to content

onError handler does not receive an actual error #451

@mdvorak

Description

@mdvorak

Prerequisites

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

Fastify version

5.7.2

Plugin version

12.5.0

Node.js version

24

Operating system

Linux

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

any

Description

Current stack can return AggregateError instead of an actual error directly, which means, method
requestImpl strips every information about original error, when passing it to onError handler (AggregateError.message is always empty).

While this case makes the problem more visible, it is a generally problematic, that handler does not receive original error at all, in any case.

Error example:

{
  "code": "ECONNREFUSED",
  "errors": [
    {
      "errno": -111,
      "code": "ECONNREFUSED",
      "syscall": "connect",
      "address": "::1",
      "port": 8080
    },
    {
      "errno": -111,
      "code": "ECONNREFUSED",
      "syscall": "connect",
      "address": "127.0.0.1",
      "port": 8080
    }
  ],
  "stack": "AggregateError [ECONNREFUSED]: \n    at internalConnectMultiple (node:net:1134:18)\n    at afterConnectMultiple (node:net:1715:7)\n    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
}

My suggestion is to pass the cause to the errors returned by this, as FastifyError already has support for this.

onError(this, { error: new InternalServerError(err.message, {cause: err}) })

I can prepare a PR, if this would be acceptable fix.

Link to code that reproduces the bug

https://github.com/mdvorak/fastify-reply-from-error

Expected Behavior

Original error should be accessible in the 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