ebec 🥋
A library that simplifies error handling by providing an ES6 error class and utility functions.
This library facilitates the extraction of options and error messages from constructor arguments.
Table of Contents
Installation
npm install ebec --save
Usage
The BaseError class accepts various constructor arguments of type Input and any
Options specified during initialization are automatically assigned as attributes.
Simple
Create error instances in different ways, as demonstrated in the following examples:
Example #1
import { BaseError } from 'ebec';
const error = new BaseError('An error occurred.');
console.log(error.message);
Example #2
In this example, only error options are passed as a single argument to the error constructor.
import { BaseError } from 'ebec';
const error = new BaseError({
message: 'The entity could not be found',
code: 'BAD_REQUEST'
});
console.log(error.message);
console.log(error.code);
Example #3
In this example, multiple arguments are passed to the error constructor.
import { BaseError } from 'ebec';
const cause = new Error('foo');
const error = new BaseError(
'The entity could not be found',
{
code: 'BAD_REQUEST'
},
cause
);
console.log(error.message);
console.log(error.code);
console.log(error.cause);
Inheritance
Custom error classes that inherit from BaseError allow for more specific error handling.
import {
BaseError,
Options
} from 'ebec';
export class NotFoundError extends BaseError {
constructor(message?: string) {
super({
message,
logMessage: true,
logLevel: 'warning',
code: 'NOT_FOUND'
});
}
}
Types
Input
type Input = Options | Error | string;
Options
type Options = {
message?: string,
stack?: string
code?: string | number | null,
data?: unknown,
expose?: boolean;
logMessage?: boolean,
logLevel?: string | number,
cause?: unknown
};
Utils
isBaseError
This method is used to determine if the error is a basic error or if the error extends this class.
import { isBaseError, BaseError } from "ebec";
const error = new BaseError();
console.log(isBaseError(error));
License
Made with 💚
Published under MIT License.