Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@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
(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')
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)
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 1,894,892 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.