Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@fastify/error
Advanced tools
A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
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.
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' });
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 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.
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 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'
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)
//@ts-expect-error
new CustomError(1)
Licensed under MIT.
FAQs
A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
The npm package @fastify/error receives a total of 369,786 weekly downloads. As such, @fastify/error popularity was classified as popular.
We found that @fastify/error demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.