A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
npm i fastify-error
The module exports a function that you can use consistent error objects, it takes 4 parameters.
createError(code, message [, statusCode [, Base]])
code
(string
, required) - The error code, you can access it later witherror.code
. For consistency, we recommend to prefix the plugins error codes withFST_
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
(Error
, optional) - The base error object that will be used. (egTypeError
,RangeError
)
const createError = require('fastify-error')
const CustomError = createError('ERROR_CODE', 'message')
console.log(new CustomError())
How to use a interpolated string:
const createError = require('fastify-error')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world')) // error.message => 'Hello world'
Licensed under MIT.