@netizen-experience/error-utils
v0.1.1
Published
A utility library for creating and managing custom error-utils with additional context and codes.
Readme
Error Utils
A utility library for creating and managing custom error-utils with additional context and codes.
Table of Contents
Installation
To install this package, run:
npm install @netizen-experience/error-utilsUsage and Examples
Creating a Custom Error
You can create a custom error by extending the BaseError class:
import { BaseError, ErrorContext } from "@netizen-experience/error-utils";
class CustomError extends BaseError {
constructor(
message: string,
options?: { prefix?: string; code?: string | number; context?: ErrorContext; cause?: Error },
) {
super(message, options);
}
}
const error = new CustomError("Something went wrong", { code: 123, context: { userId: 1 } });
console.error(error);Accessing Error Properties
The BaseError class provides additional properties like code, context, and prefix:
const error = new CustomError("Something went wrong", { code: 123, context: { userId: 1 }, prefix: "ERR" });
console.log(error.code); // 123
console.log(error.context); // { userId: 1 }
console.log(error.prefix); // 'ERR'
console.log(error.codedError()); // 'ERR 123'Using ErrorContext
The ErrorContext type defines the structure that is convertible to JSON format that can be passed to the error. This allows you to add additional information to the error, making it easier to debug and handle.
import { ErrorContext } from "@netizen-experience/error-utils";
// Adding user information to the error context
const userContext: ErrorContext = {
userId: 42,
userName: "john_doe",
userRole: "admin",
};