From 4d23f72111cd6254b6fc11a24bb34f78cf0305a0 Mon Sep 17 00:00:00 2001 From: tsm Date: Mon, 2 Oct 2017 22:00:28 +0200 Subject: [PATCH 1/2] New: Expose context Fixes https://github.com/restify/errors/issues/80 --- lib/baseClasses/HttpError.js | 8 +++++++- lib/helpers.js | 1 + test/index.js | 6 +++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/baseClasses/HttpError.js b/lib/baseClasses/HttpError.js index 283407e..297e181 100644 --- a/lib/baseClasses/HttpError.js +++ b/lib/baseClasses/HttpError.js @@ -142,10 +142,16 @@ HttpError.prototype.toJSON = function toJSON() { message = self.body.message; } - return { + var errorObject = { code: self.body.code, message: message }; + + if (self.context) { + errorObject.context = self.context; + } + + return errorObject; }; diff --git a/lib/helpers.js b/lib/helpers.js index c2ef377..58d7303 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -66,6 +66,7 @@ function parseVariadicArgs(ctorArgs, werrorSuper) { assert.optionalObject(options, 'options'); assert.optionalString(options.message, 'options.message'); assert.optionalNumber(options.statusCode, 'options.statusCode'); + assert.optionalObject(options.context, 'options.context'); } // 3 // otherwise, pass through to WError diff --git a/test/index.js b/test/index.js index df8a4b7..72b1ecc 100644 --- a/test/index.js +++ b/test/index.js @@ -596,7 +596,11 @@ describe('restify-errors node module.', function() { var expectedJSON = { code: 'Execution', message: 'bad joystick input; ' + - 'caused by Error: underlying error!' + 'caused by Error: underlying error!', + context: { + foo: 'bar', + baz: [1,2,3] + } }; assert.equal(JSON.stringify(err), JSON.stringify(expectedJSON)); assert.equal(err.toString(), err.name + ': ' + expectedJSON.message); From 6ae62cab08b87ab8114118d8fe06ccd443718d61 Mon Sep 17 00:00:00 2001 From: tsm Date: Mon, 2 Oct 2017 22:12:08 +0200 Subject: [PATCH 2/2] omit if empty object --- lib/baseClasses/HttpError.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/baseClasses/HttpError.js b/lib/baseClasses/HttpError.js index 297e181..d5fcf20 100644 --- a/lib/baseClasses/HttpError.js +++ b/lib/baseClasses/HttpError.js @@ -5,6 +5,7 @@ var util = require('util'); // external modules var WError = require('verror').WError; +var _ = require('lodash'); // internal files var helpers = require('./../helpers'); @@ -147,7 +148,7 @@ HttpError.prototype.toJSON = function toJSON() { message: message }; - if (self.context) { + if (!_.isEmpty(self.context)) { errorObject.context = self.context; }