Merge pull request #85 from dkrantsberg/safe-json-strongify
Handle Error objects with circular references
This commit is contained in:
commit
4735e34943
|
@ -5,8 +5,10 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var safeStringify = require('fast-safe-stringify');
|
||||
|
||||
module.exports = function sendJson(res, data) {
|
||||
var content = JSON.stringify({error: data});
|
||||
var content = safeStringify({error: data});
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.end(content, 'utf-8');
|
||||
};
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
"accepts": "^1.3.3",
|
||||
"debug": "^3.1.0",
|
||||
"ejs": "^2.6.1",
|
||||
"fast-safe-stringify": "^2.0.6",
|
||||
"http-status": "^1.1.2",
|
||||
"js2xmlparser": "^3.0.0",
|
||||
"strong-globalize": "^4.1.0"
|
||||
|
|
|
@ -520,6 +520,27 @@ describe('strong-error-handler', function() {
|
|||
});
|
||||
});
|
||||
|
||||
it('handles Error objects containing circular properties', function(done) {
|
||||
var circularObject = {};
|
||||
circularObject.recursiveProp = circularObject;
|
||||
var error = new ErrorWithProps({
|
||||
statusCode: 422,
|
||||
message: 'The model instance is not valid.',
|
||||
name: 'ValidationError',
|
||||
code: 'VALIDATION_ERROR',
|
||||
details: circularObject,
|
||||
});
|
||||
givenErrorHandlerForError(error, {debug: true});
|
||||
requestJson().end(function(err, res) {
|
||||
if (err) return done(err);
|
||||
expect(res.body).to.have.property('error');
|
||||
expect(res.body.error).to.have.property('details');
|
||||
expect(res.body.error.details).to.have.property('recursiveProp',
|
||||
'[Circular]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
function requestJson(url) {
|
||||
return request.get(url || '/')
|
||||
.set('Accept', 'text/plain')
|
||||
|
|
Loading…
Reference in New Issue