express-bonfire
An utility and middleware for dispatch and handling errors on express.js
We dispatch something like:
On the response we have a json with http status code 404
error: true message: 'The user is not found'
Instead of return the json error manually like:
app
// Convert this:app // To this:app
Installation
npm install -save express-bonfire
Usage
// Basic exampleconst app = const dispatchError errorMiddleware = appapp app // Adding the error middleware to the endapp // In the response we have:// status http 404 // {// error: true,// message: 'The user with id: 123 is not found'// }
// Exampleconst app = const dispatchError errorMiddleware = appapp app // Adding the error middleware to the endapp
Default separators
The default separators to split the code status http and message are |
, =>
, ->
but you can chage, creating a custom dispatcherError function.
code
You can pass code inside of parentheses eg: (7891) for handling responses on client-side
app // JSON Response: 422// Now on client-side you can handling the errorCode for 8749 "message": "Email was not included" "errorCode": 8749 "error": true
Extra Data
// Exampleconst app = const dispatchError errorMiddleware = appapp app // Adding the error middleware to the endapp // In the response we have:// {// error: true,// message: 'Email was not included',// codeErrorPath: 'email'// }
Custom Error Dispatcher
// Exampleconst app = const DispatchErrorCustom errorMiddleware = appapp // dding custom dispatch errorconst customDispatch = app // Adding the error middleware to the endapp // In the response we have:// {// error: true,// message: 'Ohh no! 🔥',// errorCode: 100// }
Custom dispatcher options
Prop Name | Type | Default | Description |
---|---|---|---|
statusHTTP | Number | 200 | Status code http for response |
errorCode | Number | 0 | Code for handling responses on client-side |
message | String | 'Something is wrong 🔥' | Error message for humans |
dividers | Array | ['|', '=>', '->'] | Characters for separate the status code http and message eg "404 => Whoops! Nos found" |
codePattern | Object RegExp | /((\s+)?(\d*)(\s+)?)/gm | Pattern to extract the errorCode |
cleanCodePattern | Object RegExp | /((|))/gm | Pattern to remove error Code to the message |
This package uses Standard JS