1.0.7 • Published 8 months ago
custom-error-handlers v1.0.7
Custom Error
A Custom Error library for daily use With Error handler
Installation
npm install custom-error-handlers
Step
1.Import Custom Error and handler\ 2.Add Error handler in the app\ 3.Throw Error\ 4.That much simple
Usage/Examples
1) Import
- All Error Class are in the
custom-error-handlers/error
folder
//import Error Class seperately
const {
AuthenticationError,
} = require('custom-error-handlers/error');
const { AuthorizationError } = require('custom-error-handlers/error');
const { BadRequestError } = require('custom-error-handlers/error');
const { ConfigurationError } = require('custom-error-handlers/error');
const { ControllerError } = require('custom-error-handlers/error');
const { DatabaseError } = require('custom-error-handlers/error');
const { NotFoundError } = require('custom-error-handlers/error');
const { PermissionError } = require('custom-error-handlers/error');
const { ValidationError } = require('custom-error-handlers/error');
//import Error Class as needed
const {
AuthenticationError,
NotFoundError,
ValidationError,
} = require('custom-error-handlers/error');
//import all Error Class together
const Error = require('custom-error-handlers/error');
- Import Error Middleware
const { CustomErrorHandler } = require('custom-error-handlers');
2) Use Error Middleware
Note add this line after using all router and Middleware
app.use(CustomErrorHandler()); //using custom-error-handlers handler
3) Throw CustomError
now throw custom error from any Middleware, Controller or Handler
throw new NotFoundError(); //for not found error
4) Nothing more you have implemented Custom Error Successfully
API Reference
CustomError
Parameter | Type | Description |
---|---|---|
errorName | string | for identifying error type |
errorMessage | string | for error message |
statusCode | number | for sending error code to the client |
errorCode | string | for identifing specific error |
All other Error Class
Note there is no errorName Parameter in these Error Class, they are already set for use
Parameter | Type | Description |
---|---|---|
errorMessage | string | for error message |
statusCode | number | for sending error code to the client |
errorCode | string | for identifing specific error |
Error Handling Middleware
Parameter | Type | Default | Description |
---|---|---|---|
options | Object | configure the middleware | |
options.passControl | boolean | false | should call next() |
options.sendResponse | boolean | true | should send response to client |
Default errorMessage and statusCode
Class | errorMessage | statusCode |
---|---|---|
AuthenticationError | Permission Needed | 403 |
AuthorizationError | Unauthorized | 401 |
BadRequestError | Bad Request | 400 |
ConfigurationError | Internal Server Error | 500 |
ControllerError | Internal Server Error | 500 |
CustomError | Internal Server Error | 500 |
DatabaseError | Service Unavailable | 503 |
NotFoundError | Not Found | 404 |
PermissionError | Permission Needed | 403 |
ValidationError | Bad Request | 400 |
Note all CustomError other than CustomError itself extends to CustomError which extends to Error
You can create new CustomError Class as you need by extending CustomError Class