1.0.7 • Published 8 months ago

custom-error-handlers v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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

ParameterTypeDescription
errorNamestringfor identifying error type
errorMessagestringfor error message
statusCodenumberfor sending error code to the client
errorCodestringfor identifing specific error

All other Error Class

Note there is no errorName Parameter in these Error Class, they are already set for use

ParameterTypeDescription
errorMessagestringfor error message
statusCodenumberfor sending error code to the client
errorCodestringfor identifing specific error

Error Handling Middleware

ParameterTypeDefaultDescription
optionsObjectconfigure the middleware
options.passControlbooleanfalseshould call next()
options.sendResponsebooleantrueshould send response to client

Default errorMessage and statusCode

ClasserrorMessagestatusCode
AuthenticationErrorPermission Needed403
AuthorizationErrorUnauthorized401
BadRequestErrorBad Request400
ConfigurationErrorInternal Server Error500
ControllerErrorInternal Server Error500
CustomErrorInternal Server Error500
DatabaseErrorService Unavailable503
NotFoundErrorNot Found404
PermissionErrorPermission Needed403
ValidationErrorBad Request400

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

Author

@github-MHRSRoni\ @Mail me

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago