node-server-error
v0.3.0
Published
Server Error Class
Downloads
2
Readme
node-server-error
Server Error Class
Install
$ npm install node-server-error --save
Overview
ServerError(properties)
Properties
status
(Number) - HTTP status response, defaults to500
.type
(String) - Some unique code that identifies the error, defaults to'E_UNKNOWN'
.code
(Number) - Number code for unique identify error, defaults to0
.message
(String) - A human readable message describing the error, defaults to'Encountered an unexpected error'
.
Usage
const ServerError = require('node-server-error');
// Create ServerError instance
let serverError = new ServerError();
serverError.toString();
// Output
// [ServerError (E_UNKNOWN:0) Encountered an unexpected error]
serverError.toJSON();
// Output
// {
// status: 500,
// type: 'E_UNKNOWN',
// code: 0,
// message: 'Encountered an unexpected error'
// }
Default properties
if (/* some condition that means an error */) {
throw new ServerError();
}
Override default properties
if (/* some condition that means an error */) {
throw new ServerError({
status: 401,
type: 'E_UNAUTHORIZED',
code: 8520, // It could mean a unique error code.
message: 'Unauhtorized access!'
});
}
Test
$ npm test