Responserror
Express Error Handler to Node.js apps
Responserror is an Error Handler middleware for express.
Installation
The latest version is available at: https://www.npmjs.com/package/responserror
Use your favorite package manager to install. For instance:
yarn add responserror
Then import it and initialize a new instance:
import Responserror from 'responserror'
const { errorHandler } = new Responserror()
Then, you will want to add the errorHandler
as your last express middleware:
app.use(authRouter, userRouter, errorHandler)
And you're good to go!
Usage Example
import express from 'express'
import Responserror from 'responserror'
const app = express()
const router = express.Router()
const { errorHandler } = new Responserror()
router.post('/users', (_, response: Response, next: NextFunction) => {
try {
throw {
code: 504,
}
} catch(err) {
return next(err)
}
})
app.use(router, errorHandler)
Outputs HTTP Status 504 and following JSON response:
{
code: 504,
status: 'GATEWAY_TIMEOUT',
message: 'Gateway Timeout',
success: false
}
-
code: if given responserror
will try to find its status
and message
automatically.
-
status: if given responserror
will try to find its code
and message
automatically.
-
success: will always be false
, unless specified otherwise.
-
message: will be filled automatically if given code
or status
are valid and no message
value is given.
-
errors: anything can be sent here, responserror
will not try to fill this.
Note: if message
is given a value, that will override the automatic value responserror
would give. This applies to all other properties.
All properties are optional as shown in the first example.
In this example, we send message
and errors
as well:
router.post('/users', (_, response: Response, next: NextFunction) => {
try {
throw {
code: 400,
message: 'Sorry!! Your request is invalid! =/',
errors: [
{ name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
]
}
} catch(err) {
return next(err)
}
})
Outputs HTTP Status 400 and following JSON response:
{
code: 400,
status: 'BAD_REQUEST',
message: 'Sorry!! Your request is invalid! =/',
errors: [
{ name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
]
}
Hooks
Use method pre
to execute functions before sending the response:
const app = express()
app.use(responser)
const responserror = new Responserror({ promptErrors: true })
const errorHandler = responserror.errorHandler
responserror.pre(() => {
console.info('This will execute before the error response is given!')
})
const router = express.Router()
app.use(router, errorHandler)
While pre
will execute as the first thing responserror
will do, method pos
will execute as the last thing responserror will do before sending the response.
Status & Codes
100
101
102
200
201
202
203
204
205
206
207
300
301
302
303
304
305
307
308
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
422
423
424
428
429
431
451
500
501
502
503
504
505
507
511
Check the updated list of http status codes for all status and codes available.
Responser
If you want to have all HTTP responses at the tip of your finger (including sucessful ones), be sure to check out responser npm package. Differently from responserror
which is the "catch-all" error handler for express, responser
is used to directly send responses, wherever middleware/controller you are. Both responser
and responserror
can be used together.
If you are using responser module in the same express instance, responserror
will invoke send_* methods instead of its own.
Testing
Run the test suit with yarn test
.
Contributing
If you want to contribute in any of theses ways:
- Give your ideas or feedback
- Question something
- Point out a problem or issue
- Enhance the code or its documentation
- Help in any other way
You can (and should) open an issue or even a pull request!
Thanks for your interest in contributing to this repo!
Author
Luiz Felipe Zarco (felipezarco@hotmail.com)
License
This code is licensed under the MIT License. See the LICENSE.md file for more info.