From 0ce15b2d272124291d6b15a43134767296a35ae9 Mon Sep 17 00:00:00 2001 From: Zak Barbuto Date: Mon, 5 Mar 2018 14:09:51 +1030 Subject: [PATCH] Undefined safeFields revert to data #71 --- lib/data-builder.js | 4 +++- test/handler.test.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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',