1.1.2 • Published 4 years ago
exception-library v1.1.2
ExceptionLibrary Documentation
NPM Library to handle different kind of api exceptions in a Express app
Installation
Install library using npm package manager
npm install exception-libraryUsage
Add ExceptionHandler middleware to your express application
import express from "express";
import { ExceptionHandler } from "exception-library";
const app = express();
// After adding your route handlers
app.use(ExceptionHandler);After this throw different types errors as your needs inside a route
Supported Exceptions
Bad Gateway Exception
router.get('/path', (req, res) => {
...
throw new BadGatewayError("some reason")
...
})Bad Request Exception
router.get('/path', (req, res) => {
...
throw new BadRequestError("some reason")
...
})Forbiddon Exception
router.get('/path', (req, res) => {
...
throw new ForbiddenError("some reason")
...
})Internal Server Exception
router.get('/path', (req, res) => {
...
throw new InternalServerError("some reason")
...
})Method not allowed Exception
router.get('/path', (req, res) => {
...
throw new MethodNotAllowedError("some reason")
...
})Not found Exception
router.get('/path', (req, res) => {
...
throw new NotFoundError("some reason")
...
})Not Implemented Exception
router.get('/path', (req, res) => {
...
throw new NotImplementedError("some reason")
...
})Request Time Out Exception
router.get('/path', (req, res) => {
...
throw new RequestTimeOutError("some reason")
...
})Unauthorized Exception
router.get('/path', (req, res) => {
...
throw new UnorthorizedError("some reason")
...
})Data Validation Exception with Joi
router.get('/path', (req, res) => {
...
// Only support with Joi validation
const errors = [{
message: detail.message,
field: detail.context.label,
errorType: detail.type
}]
throw new DataValidationError(errors)
...
})Data Types
Return Error Type
Exceptions returns from API like follwoing format
interface ReturnErrorType<Type> {
statusCode: number
errors: Type[]
type: string
}Validation Error Type
Data should be in this type to pass to the DataValidationError
interface ValidationErrorType {
message: string;
field: string | undefined;
errorType: string;
}