Package Exports
- ebec
- ebec/package.json
Readme
ebec 🥋
A library that provides a basic ES6 error class and helper functions for extracting options and the error message from any number of constructor arguments.
Table of Contents
Installation
npm install ebec --saveUsage
Simple
The BaseError class can be initialized on different ways, like demonstrated by the following examples:
Example #1
In this example no options are specified on class instantiation, but afterward.
import { BaseError } from 'ebec';
const error = new BaseError('An error occurred.');
console.log(error.message);
// An error occurred.Example #2
In the following example only the error options are passed as single argument to the error constructor.
import { BaseError, Options } from 'ebec';
const error = new BaseError({
message: 'The entity could not be found',
code: 'BAD_REQUEST'
});
console.log(error.message);
// The entity could not be found
// access the option values
console.log(error.code);
// BAD_REQUESTInheritance
Besides, using only the BaseError class, own classes which inherit the BaseError class, can simply be created and provide a better way to handle errors more differentiated.
import {
BaseError,
Options
} from 'ebec';
export class NotFoundError extends BaseError {
constructor(message?: string) {
super({
logMessage: true,
logLevel: 'warning',
code: 'NOT_FOUND'
});
}
}Types
Options
type Options = {
/**
* The actual error message, if not provided on another way.
*/
message?: string,
/**
* The error code is either a short uppercase string identifier
* for the error or a numeric error code. For example: SERVER_ERROR
*/
code?: string | number | null,
/**
* Can the error message be exposed externally without hesitation
* or is it restricted for internal use?
*/
expose?: boolean;
/**
* Should the error be logged?
*/
logMessage?: boolean,
/**
* Set the log level for this error.
*/
logLevel?: string | number,
/**
* A cause for the error.
*/
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));
// trueLicense
Made with 💚
Published under MIT License.