customizable-error
v0.1.3
Published
Customizable error to pass extra fields in your errors
Downloads
3
Readme
Customizable error
Description
Let you define an error with custom fields to pass extra informations with message
Installation
npm i --save customizable-error
Using the factory
import { customErrorFactory } from 'customizable-error';
// some code that throw an error
(req, res, next) => {
next(
customErrorFactory({
name: 'SuperError',
code: 'SUPER_ERROR',
message: 'this is an error message',
status: 500,
foo: 'bar',
baz: 'bat',
}),
);
}
// some code that handles the error
const errorHandler = (err, req, res, next) => {
res.status(err.getStatus()).json({
success: false,
message: err.getMessage(), // 'this is an error message'
code: err.getCode(), // 'SUPER_ERROR'
data: err.getExtraFields(), // { foo: 'bar', baz: 'bat' }
});
};
Extending the class
import { CustomError, ErrorOptions } from 'customizable-error';
class SuperError extends CustomError {
constructor() {
super({
name: 'SuperError',
code: 'SUPER_ERROR',
message: 'this is an error message',
status: 500,
foo: 'bar',
baz: 'bat',
})
}
}
// some code that throw an error
(req, res, next) => {
next(SuperError());
}
// some code that handles the error
const errorHandler = (err, req, res, next) => {
res.status(err.getStatus()).json({
success: false,
message: err.getMessage(), // 'this is an error message'
code: err.getCode(), // 'SUPER_ERROR'
data: err.getExtraFields(), // { foo: 'bar', baz: 'bat' }
});
};