What is @fastify/error?
The @fastify/error package is designed to provide a simple and efficient way to create custom errors in Fastify applications. It allows developers to define error constructors with default status codes and messages, making error handling more consistent and streamlined across the application.
What are @fastify/error's main functionalities?
Creating custom errors
This feature allows developers to create custom error constructors. The example demonstrates how to create a 'NotFoundError' with a default message 'Resource not found' and a status code of 404.
const createError = require('@fastify/error');
const NotFoundError = createError('NOT_FOUND', 'Resource not found', 404);
throw new NotFoundError();
Custom error properties
This feature enables adding custom properties to errors. In the example, a 'DatabaseError' is created with an additional 'code' property, which is specified when the error is thrown.
const createError = require('@fastify/error');
const DatabaseError = createError('DB_ERROR', 'Database operation failed', 500, (opts) => ({ code: opts.code }));
throw new DatabaseError({ code: 'ER_NO_SUCH_TABLE' });
Other packages similar to @fastify/error
http-errors
Similar to @fastify/error, http-errors is a package for generating HTTP errors for Node.js web applications. While @fastify/error is tailored for Fastify applications, http-errors is more generic and can be used with any Node.js web framework.
boom
Boom provides a set of utilities for returning HTTP errors. It offers more predefined errors compared to @fastify/error. However, @fastify/error allows for more customization in defining error constructors with default messages and status codes.
@fastify/error
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'))
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')
new CustomError(1)
new CustomError(1)
License
Licensed under MIT.