express-mung
Advanced tools
Comparing version 0.3.1 to 0.4.1
41
index.js
@@ -10,2 +10,7 @@ 'use strict'; | ||
mung.onError = (err, req, res) => { | ||
res.status(500).json({ message: err.message }).end(); | ||
return res; | ||
}; | ||
mung.json = function json (fn) { | ||
@@ -19,3 +24,9 @@ return function (req, res, next) { | ||
return res; | ||
json = fn(json, req, res); | ||
// Run the munger | ||
try { | ||
json = fn(json, req, res); | ||
} catch (e) { | ||
return mung.onError(e, req, res); | ||
} | ||
if (res.headersSent) | ||
@@ -53,3 +64,4 @@ return res; | ||
return; | ||
fn(json, req, res) | ||
try { | ||
fn(json, req, res) | ||
.then(json => { | ||
@@ -70,3 +82,7 @@ if (res.headersSent) | ||
return original.call(this, json); | ||
}); | ||
}) | ||
.catch(e => mung.onError(e, req, res)); | ||
} catch (e) { | ||
mung.onError(e, req, res); | ||
} | ||
@@ -87,3 +103,7 @@ return faux_fin; | ||
if (!res.headersSent) { | ||
fn(req, res); | ||
try { | ||
fn(req, res); | ||
} catch (e) { | ||
return mung.onError(e, req, res); | ||
} | ||
if (res.headersSent) { | ||
@@ -105,2 +125,6 @@ console.error('sending response while in mung.headers is undefined behaviour'); | ||
let original = res.end; | ||
let onError = e => { | ||
res.end = original; | ||
mung.onError(e, req, res); | ||
}; | ||
function hook () { | ||
@@ -111,3 +135,4 @@ if (res.headersSent) | ||
res.end = () => null; | ||
fn(req, res) | ||
try { | ||
fn(req, res) | ||
.then(() => { | ||
@@ -118,3 +143,7 @@ res.end = original; | ||
original.apply(this, args); | ||
}); | ||
}) | ||
.catch(e => onError(e, req, res)); | ||
} catch (e) { | ||
onError(e, req, res); | ||
} | ||
} | ||
@@ -121,0 +150,0 @@ res.end = hook; |
{ | ||
"name": "express-mung", | ||
"version": "0.3.1", | ||
"version": "0.4.1", | ||
"description": "Transform an express response (or make until no good)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,5 +35,5 @@ # express-mung [![Build Status](https://travis-ci.org/richardschneider/express-prefer.svg)](https://travis-ci.org/richardschneider/express-mung) | ||
then add to your `server.js` file | ||
then add to your `app.js` file (before the route handling middleware) | ||
````javascript | ||
app.use(require('redact.js')) | ||
app.use(require('./redact')) | ||
```` | ||
@@ -60,2 +60,6 @@ and [*That's all folks!*](https://www.youtube.com/watch?v=gBzJGckMYO4) | ||
## Exception handling | ||
`mung` catches any exception (synchronous, asynchronous or Promise reject) and sends an HTTP 500 response with the exception message. This is done by `mung.onError(err, req, res)`, feel free to redefine it to your needs. | ||
# License | ||
@@ -62,0 +66,0 @@ The MIT license |
@@ -47,2 +47,31 @@ 'use strict'; | ||
it('should 500 on a synchronous exception', done => { | ||
function error (req, res) { | ||
req.hopefully_fails(); | ||
} | ||
let server = express() | ||
.use(mung.headers(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(500) | ||
.end(done); | ||
}); | ||
it('should 500 on an asynchronous exception', done => { | ||
function error (req, res) { | ||
return Promise.resolve(true) | ||
.then(() => { | ||
req.hopefully_fails(); | ||
}); | ||
} | ||
let server = express() | ||
.use(mung.headersAsync(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(500) | ||
.end(done); | ||
}); | ||
}) |
@@ -22,2 +22,6 @@ 'use strict'; | ||
function error(json, req, res) { | ||
json.foo.bar.hopefully.fails(); | ||
} | ||
it('should return the munged JSON result', done => { | ||
@@ -94,2 +98,28 @@ let server = express() | ||
it('should 500 on a synchronous exception', done => { | ||
let server = express() | ||
.use((err, req, res, next) => res.status(500).send(err.message).end()) | ||
.use(mung.json(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(500) | ||
.end(done); | ||
}); | ||
it('should 500 on an asynchronous exception', done => { | ||
let server = express() | ||
.use((err, req, res, next) => res.status(500).send(err.message).end()) | ||
.use(mung.json(error)) | ||
.get('/', (req, res) => { | ||
process.nextTick(() => { | ||
res.status(200).json({ a: 'a' }).end(); | ||
}); | ||
}); | ||
request(server) | ||
.get('/') | ||
.expect(500) | ||
.end(done); | ||
}); | ||
}) |
@@ -29,2 +29,6 @@ 'use strict'; | ||
function error(json, req, res) { | ||
return Promise.resolve(json) | ||
.then(json => json.foo.bar.hopefully.fails()) | ||
} | ||
@@ -88,2 +92,13 @@ it('should return the munged JSON result', done => { | ||
it('should 500 on an exception', done => { | ||
let server = express() | ||
.use((err, req, res, next) => res.status(501).send(err.message).end()) | ||
.use(mung.jsonAsync(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(500) | ||
.end(done); | ||
}); | ||
}) |
19126
395
67