req-error
v2.0.2
Published
This package makes catch async & custom error concepts in any app easier.
Downloads
56
Readme
req-error
This package makes catch async & custom error concepts in any app easier.
Features
- TypeScript full support
- Custom Error with message and statusCode
- Error catcher function with a geart durablity
- Error handler with common situations
Installation
- with npm
npm i req-error
- with yarn
yarn add req-error
- with pnpm
pnpm add req-error
Express
Configure your application with whatever configuration you want.
Basic Usage:
/* controller.js */
// Make ReqError global (Recommended)
require('req-error/global')
const { catchError } = require('req-error')
// Not Global (Not Recommended)
const { default: ReqError, catchError } = require('req-error')
const login = catchError((req, res) => {
// Do your stuff...
throw new ReqError('This is just a demo', 400)
})
const signup = catchError(async (req, res) => {
// Do your stuff...
throw new ReqError('This is just another demo', 401)
})
Some possible usages of ReqError
:
new ReqError('Message', 404)
// { message: "Message", statusCode: 404 }
new ReqError(['Message', 404])
// { message: "Message", statusCode: 404 }
new ReqError(['Message', 404], 500)
// { message: "Message", statusCode: 500 }
// Even more simple:
throw 'Error message'
throw ['Error message', 404]
Some possible usages of catchError
:
// catchError(arg,...)
catchError(Function)
// Function
catchError(Function, String)
// [Function, String]
catchError(Function, Function, Function)
// [Function, Function, Function]
catchError([Function])
// [Function]
catchError([Function, Function], Function)
// [[Function, Function], Function]
catchError({ login: Function })
// { login: Function }
catchError(require('./login.js'))
// { login: Function }
catchError(require('./login.js'), require('./signup.js'))
// [{ login: Function }, { signup: Function }]
catchError(Function, [Function, Function], { login: Function })
// [Function, [Function, Function], { login: Function }]
Made by Nazmus Sayad with ❤️.