4.0.0 • Published 12 months ago

@fastify/error v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

@fastify/error

CI NPM version js-standard-style

A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.

Install

npm i @fastify/error

Usage

The module exports a function that you can use for consistent error objects, it takes 4 parameters:

createError(code, message [, statusCode [, Base]])
  • code (string, required) - The error code, you can access it later with error.code. For consistency, we recommend prefixing plugin error codes with FST_
  • message (string, required) - The error message. You can also use interpolated strings for formatting the message.
  • statusCode (number, optional) - The status code that Fastify will use if the error is sent via HTTP.
  • Base (ErrorConstructor, optional) - The base error object that will be used. (eg TypeError, RangeError)
const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello')
console.log(new CustomError()) // error.message => 'Hello'

How to use an interpolated string:

const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world')) // error.message => 'Hello world'

How to add cause:

const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world', {cause: new Error('cause')})) 
// error.message => 'Hello world'
// error.cause => Error('cause')

TypeScript

It is possible to limit your error constructor with a generic type using TypeScript:

const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
new CustomError('world')
//@ts-expect-error
new CustomError(1)

License

Licensed under MIT.

@everything-registry/sub-chunk-315@comapeo/core@bram-dc/fastify-type-provider-zod@ayazmo/utils@devoplx/mediumplugin@platformatic/client-cli@fastify-extended/middie@fastify/reply-from@fastify/multipart@fastify/jwt@fastify/middie@fastify/circuit-breaker@fastify/csrf-protection@fastify/under-pressure@fastify/basic-auth@fastify/bearer-auth@fibus-digital/sluby@lynithdev/fastify-filerouting@mahhoura/fastify-autoroutes@mapeo/core@mapeo/map-server@mercuriusjs/federation@mercuriusjs/subscription-client@jvddavid/fastify-autoroutes@iimm/fastify-multipart@ogcio/fastify-logging-wrapper@platformatic/deploy-client@platformatic/generators@platformatic/itc@platformatic/metaconfig@platformatic/utils@platformatic/service@platformatic/shared@platformatic/sql-events@platformatic/sql-graphql@platformatic/sql-mapper@platformatic/sql-openapi@platformatic/composer@platformatic/config@platformatic/control@platformatic/db@platformatic/db-authorization@platformatic/runtime@psteinroe/fastify-supabase@platformatic/authenticate@platformatic/basic@platformatic/bus@platformatic/client@platformatic/ai-warp@ordinal/cleo@silvertree/httpmr-pokenpm-packages-offlineplatformaticmercuriusmercurius-fetchmercurius-validationmercurius-authavviodo-functionsfastify-grammyfastify-hashidsfastify-json5fastify-knex-apifastify-https-alwaysfastify-hl7fastify-tmafastify-type-provider-zodfastify-zod-openapifastify-zod-query-coercionfastify-userfastify-uwsfastify-commonfastify-crud-generatorfastify-citgmfastify-autoroutesfastifyfastify-openid-authfastify-multipart-firebasefastify-rabbitmqfastify-racing
4.0.0

12 months ago

3.4.0

2 years ago

3.4.1

2 years ago

3.3.0

2 years ago

3.2.1

2 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago