1.0.0 • Published 1 year ago

@akrdevtech/lib-error-handler-middleware v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Express Global Error Handler Middleware

Description

A custom error handler middleware for ExpressJS applications with TS.

  • TypeScript support.
  • Generic Error Support

Quick Links

Usage

Install

npm i @akrdevtech/lib-error-handler-middleware

Example Usage (TypeScript)

import * as express from 'express'
import { errorHandler } from  '@akrdevtech/lib-error-handler-middleware';

const app = express()

app.get('/', (req, res) => { res.send(`Hello World!`) } );
// your app routes goes here ... 

app.use(errorHandler)

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Example Usage (JavaScript)

const app = require('express')()
const { errorHandler } = require('@akrdevtech/lib-error-handler-middleware');

app.get('/', (req, res) => { res.send(`Hello World!`) } );
// your app routes goes here ... 

app.use(errorHandler)

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Generic Error Support

Make use of the common generic errors that are shipped with this package to handle some common error scenarios in the application. Some of them are as follows...

  • BaseError - Implement your custom error extending this base
  • NotFoundError
  • DatabaseError
  • InternalError
  • ValidationError
  • AccessDeniedError
  • ConfigurationError
  • InvalidArgumentError
  • RequestValidationMiddlewareError

Generic Error Support Usage Example

const app = require('express')()
const { 
	errorHandler,
	ConfigurationError 
} = require('@akrdevtech/lib-error-handler-middleware');

app.get('/', (req, res, next) => { 
  try {
      exampleService();
      res.send(`Hello World!`)
    } catch (error) {
      next(error)
    } 
});
// your app routes goes here ... 

const exampleService = () =>{
	// throwing custom errors
	throw new ConfigurationError('Some configuration error message')
}

app.use(errorHandler)

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Custom Error Support

Define you own custom errors by extending the BaseError in the package.

Define Custom Errors

import { 
  ErrorSource,
  HttpStatusCode,
  BaseError 
} from '@akrdevtech/lib-error-handler-middleware';

export class MyCustomError extends BaseError {
  constructor(message: string) {
    super(ErrorSource.INTERNAL, message);
    this.statusCode = HttpStatusCode.BAD_REQUEST;
    Object.setPrototypeOf(this, MyCustomError.prototype);
  }
}

Custom Error Support Usage Example

import * as express from 'express'
import { errorHandler } from  '@akrdevtech/lib-error-handler-middleware';
import { MyCustomError } from '../path/to/custom/error/file';

const app = express()

app.get('/', (req, res, next) => { 
  try {
      exampleService();
      res.send(`Hello World!`)
    } catch (error) {
      next(error)
    } 
});
// your app routes goes here ... 

const exampleService = () =>{
	// throwing custom errors
	throw new MyCustomError('Some configuration error message')
}

app.use(errorHandler)

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);