Merge pull request #85 from dkrantsberg/safe-json-strongify

Handle Error objects with circular references
This commit is contained in:
Miroslav Bajtoš 2019-09-30 08:10:59 +02:00 committed by GitHub
commit 4735e34943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -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');
};

View File

@ -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"

View File

@ -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')