A bundle of error classes
-
Install the package by npm:
npm install yan-error-class
-
Import the error class and throw the instance
import {AuthError} from 'yan-error-class'; throw new AuthError('Invalid token'); // or throw new AuthError({ message: 'Invalid token' code: 101101 // you can define custom code status: 401 // http status code, extra: {user: 'admin'} // the extra object will be returned in http response body id: '1' // you can add custom key });
-
When you need a http response about a error:
import {AuthError} from 'yan-error-class'; let error = new AuthError('Invalid token'); console.log(error.getHttpError()) // {code: 101101, message: 'Invalid token', extra: undefined}
Use import
to use these error class.
import {ErrorType} from 'yan-error-class';
The code
can be defined as you need.
usage: authentication failure
default code: 108
status: 401
usage: bad url
default code: 102
status: 400
usage: the cloud is unavailable
default code: 103
status: 503
usage: get server config error
default code: 104
status: 409
usage: operate a resource in invalid status
default code: 107
status: 409
usage: database error
default code: 105100
status: 503
usage: no permission to access a resource
default code: 101
status: 403
usage: basic infrastructure error
default code: 105
status: 503
usage: internal error
default code: 100
status: 500
usage: a resource is not existed
default code: 103
status: 404
usage: invalid request body
default code: 102
status: 400
usage: the resource is unavailable
default code: 100
status: 503
usage: the action is not supported
default code: 103
status: 503
import _ from 'lodash';
import {AbstractError} from 'yan-error-class';
export default class CustomError extends AbstractError {
constructor(obj) {
super(_.defaults(obj, {
status: 400,
code: 100000
}));
}
}