fix: handle Error objects with circular properties
This commit is contained in:
parent
7dabb649a3
commit
4b3c802088
|
@ -5,8 +5,10 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var safeStringify = require('fast-safe-stringify');
|
||||||
|
|
||||||
module.exports = function sendJson(res, data) {
|
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.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||||
res.end(content, 'utf-8');
|
res.end(content, 'utf-8');
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
"accepts": "^1.3.3",
|
"accepts": "^1.3.3",
|
||||||
"debug": "^3.1.0",
|
"debug": "^3.1.0",
|
||||||
"ejs": "^2.6.1",
|
"ejs": "^2.6.1",
|
||||||
|
"fast-safe-stringify": "^2.0.6",
|
||||||
"http-status": "^1.1.2",
|
"http-status": "^1.1.2",
|
||||||
"js2xmlparser": "^3.0.0",
|
"js2xmlparser": "^3.0.0",
|
||||||
"strong-globalize": "^4.1.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) {
|
function requestJson(url) {
|
||||||
return request.get(url || '/')
|
return request.get(url || '/')
|
||||||
.set('Accept', 'text/plain')
|
.set('Accept', 'text/plain')
|
||||||
|
|
Loading…
Reference in New Issue