express_cowrap

1.0.2 • Public • Published

Build Status Code Climate Issue Count Test Coverage

express_cowrap

Wraps Express middleware functions so you can use proper error handling for applications depending heavily on Promises. Examples:

 
const Promise = require('bluebird')
const coWrap = require('express_cowrap')
const express = require('express')
 
let app = express()
 
// Promise-returing function
app.get('/promise', coWrap(function (req, res) {
    return Promise.reject('error message')
})
 
// Regular, synchronous function
app.get('/sync', coWrap(function (req, res) {
    throw new Error('error message')
}))
 
// Generator function (with yield)
app.get('/generators', coWrap(function* (req, res) {
    let data = yield database.query() // promise-returning function
    yield Promise.delay(20)
    
    throw new Error('error message')
    
    // Won't execute this
    res.status(200).send({ message: "You won't see me!" })
}))
 
// Error handler
app.use(function errorHandler(err, req, res, next) {
    console.log('[Error happened]', err)
    
    res.status(500).send({ error: (err.message || err) })
})
 

Package Sidebar

Install

npm i express_cowrap

Weekly Downloads

1

Version

1.0.2

License

ISC

Last publish

Collaborators

  • danp3d