diff --git a/lib/data-builder.js b/lib/data-builder.js index 5505d31..47e3a29 100644 --- a/lib/data-builder.js +++ b/lib/data-builder.js @@ -91,6 +91,8 @@ function fillSafeFields(data, err, safeFields) { } safeFields.forEach(function(field) { - data[field] = err[field]; + if (err[field] !== undefined) { + data[field] = err[field]; + } }); } diff --git a/test/handler.test.js b/test/handler.test.js index 5d6b44f..c8b9ffd 100644 --- a/test/handler.test.js +++ b/test/handler.test.js @@ -300,6 +300,24 @@ describe('strong-error-handler', function() { }); }); + it('safe fields falls back to existing data', function(done) { + var error = new ErrorWithProps({ + name: 'Error', + isSafe: false, + }); + givenErrorHandlerForError(error, { + safeFields: ['statusCode', 'isSafe'], + }); + + requestJson().end(function(err, res) { + if (err) return done(err); + expect(res.body.error.statusCode).to.equal(500); + expect(res.body.error.isSafe).to.equal(false); + + done(); + }); + }); + it('should allow setting safe fields when status=4xx', function(done) { var error = new ErrorWithProps({ name: 'Error',