boomware
Opinionated Boom-based wrapper
for asynchronous middleware (or handler) for Express, Connect, router, etc.
Installing
npm i --save boomware
Example
const boom = require('boom')
const boomware = require('boomware')
const express = require('express')
const app = express()
app.get('/async', boomware(async (req, res) => {
if (Math.random() > 0.75) throw('Unexpected error!')
if (Math.random() > 0.5) throw boom.serverUnavailable()
res.send('OK!')
}))
app.use((err, req, res, next) => {
console.error(err)
res.status(err.output.statusCode).json(err.output.payload)
})
app.listen(3000)
API
boomware(fn)
Returns a middleware function that catches a thrown error, wraps it
in an Boom.badImplementation
if it's not already a Boom error, and passes it to the next()
function. boomware
handles the case whenfn
return Promise, and the
case when fn
does not return a Promise.
Related work
License
MIT