Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jkyberneees/http-error-handler
Node.js framework agnostic HTTP error handler
https://github.com/jkyberneees/http-error-handler
Last synced: about 1 month ago
JSON representation
Node.js framework agnostic HTTP error handler
- Host: GitHub
- URL: https://github.com/jkyberneees/http-error-handler
- Owner: jkyberneees
- License: mit
- Created: 2020-02-25T09:54:07.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-30T07:47:19.000Z (over 4 years ago)
- Last Synced: 2024-10-12T13:38:34.844Z (2 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- cuban-opensource - agnostic-http-error-handler
README
# agnostic-http-error-handler
Node.js framework agnostic HTTP error handler.## Usage
### Express.js
```js
const { express: errMiddleware } = require('agnostic-http-error-handler')()const service = require('express')()
service.get('/throw', (req, res) => {
throw new Error('Upps!')
})service.use(errMiddleware)
service.listen(3000)
```### Restana
```js
const { restana: errorHandler } = require('agnostic-http-error-handler')()
const service = require('restana')({
errorHandler
})service.get('/throw', (req, res) => {
throw new Error('Upps!')
})service.start()
```
### Native
```js
const { native: errorHandler } = require('agnostic-http-error-handler')()
...
server.on('request', (req, res) => {
try {
// request handler logic...
} catch (err) {
errorHandler(err, req, res)
}
})
```### Pre-Error-Handler callback
In case you would like to extend the error processing logic of your application, you can use the `preErrorHandler` configuration callback:
```js
const logger = require('...')
const { native: errorHandler } = require('agnostic-http-error-handler')((err, responsePayload, req, res) => {
logger.error(`Unexpected error while processing "${req.method} ${req.url}" request`, err)
})...
server.on('request', (req, res) => {
try {
// request handler logic...
} catch (err) {
errorHandler(err, req, res)
}
})
```> NOTE: In case the `preErrorHandler` invocation response can be coerce as a TRUE value, sending the `responsePayload` to the client is aborted.
## Sponsors
- Kindly sponsored by [ShareNow](https://www.share-now.com/), a company that promotes innovation!