Skip to content

learnwiz/fastify-error

This branch is 53 commits behind fastify/fastify-error:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e4b4bd1 · Dec 2, 2022

History

83 Commits
Aug 17, 2022
Aug 18, 2022
Dec 2, 2022
Jun 10, 2022
Aug 17, 2022
Aug 17, 2022
Jun 21, 2020
May 23, 2022
Dec 2, 2022
Dec 2, 2022

Repository files navigation

@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 (Error, optional) - The base error object that will be used. (eg TypeError, RangeError)
const createError = require('@fastify/error')
const CustomError = createError('ERROR_CODE', 'message')
console.log(new CustomError())

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'

License

Licensed under MIT.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 93.9%
  • TypeScript 6.1%