Skip to content

Commit 13498c9

Browse files
authored
Merge pull request #69 from osher/patch-3
json_error_handler: cfg.includeErrStack
2 parents e89bdf2 + 215de08 commit 13498c9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

fittings/json_error_handler.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module.exports = function create(fittingDef, bagpipes) {
3535

3636
context.headers['Content-Type'] = 'application/json';
3737
Object.defineProperty(err, 'message', { enumerable: true }); // include message property in response
38+
if (fittingDef.includeErrStack)
39+
Object.defineProperty(err, 'stack', { enumerable: true }); // include stack property in response
3840

3941
delete(context.error);
4042
next(null, JSON.stringify(err));

test/fittings/json_error_handler.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ describe('json_error_handler', function() {
123123
});
124124
});
125125
});
126+
127+
describe('includeErrStack:true', function() {
128+
129+
var context;
130+
beforeEach(function() {
131+
var err = new Error('this is a test');
132+
err.statusCode = 401;
133+
err.someAttr = 'value';
134+
context = {
135+
headers: {},
136+
error: err
137+
};
138+
});
139+
140+
it('should allow the stack in the response body', function(done) {
141+
142+
var jsonErrorHandler = json_error_handler({ includeErrStack: true });
143+
144+
jsonErrorHandler(context, function(err, output) {
145+
should.not.exist(err);
146+
should.not.exist(context.error);
147+
148+
var e;
149+
try {
150+
var body = JSON.parse(output);
151+
body.should.have.property('message', 'this is a test');
152+
body.should.have.property('someAttr','value');
153+
body.should.have.property('stack')
154+
} catch(x) { e = x }
155+
done(e)
156+
});
157+
})
158+
})
126159

127160
describe('handle500Errors:true and error fails to stringify', function() {
128161
var jsonErrorHandler;

0 commit comments

Comments
 (0)