Skip to content

Commit

Permalink
Merge pull request apigee-127#69 from osher/patch-3
Browse files Browse the repository at this point in the history
json_error_handler: cfg.includeErrStack
  • Loading branch information
theganyo authored Nov 7, 2016
2 parents e89bdf2 + 215de08 commit 13498c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fittings/json_error_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = function create(fittingDef, bagpipes) {

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

delete(context.error);
next(null, JSON.stringify(err));
Expand Down
33 changes: 33 additions & 0 deletions test/fittings/json_error_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ describe('json_error_handler', function() {
});
});
});

describe('includeErrStack:true', function() {

var context;
beforeEach(function() {
var err = new Error('this is a test');
err.statusCode = 401;
err.someAttr = 'value';
context = {
headers: {},
error: err
};
});

it('should allow the stack in the response body', function(done) {

var jsonErrorHandler = json_error_handler({ includeErrStack: true });

jsonErrorHandler(context, function(err, output) {
should.not.exist(err);
should.not.exist(context.error);

var e;
try {
var body = JSON.parse(output);
body.should.have.property('message', 'this is a test');
body.should.have.property('someAttr','value');
body.should.have.property('stack')
} catch(x) { e = x }
done(e)
});
})
})

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

0 comments on commit 13498c9

Please sign in to comment.