Skip to content

should this package expose FastifyError? #17

@ahmadnassri

Description

@ahmadnassri

🚀 Feature Proposal

should export FastifyError function so it can be used with err instanceOf FastifyError in plain JavaScript

is there a reason this is not currently exposed? seems like the use-case is primarily focused on TypeScript, any reason we shouldn't have access to it for plain JS.

Motivation

in a Fastify errorHandler, you want to intercept errors and check what they are

Example

const { FastifyError } = require('fastify-error')
const { DatabaseError } = require('pg-protocol/dist/messages')

module.exports = function (error, request, reply) {
    // write to log
    request.log.fatal({ ...error }, error.message)

    if (error instanceOf FastifyError) {
        // do stuff
    }

    if (error instanceof SyntaxError) {
        // do other stuff
    }

    if (error.validation) {
        // handle input validation errors
    }

    if (error instanceof DatabaseError) {
        // replace db query errors with public friendly ones
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions