From f3bf5e08d048ebe2e18319914241209aa763bb69 Mon Sep 17 00:00:00 2001 From: spalger Date: Wed, 16 Nov 2016 16:10:04 -0700 Subject: [PATCH] [errors] fix printing of objects in error data --- src/lib/errors.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/lib/errors.js b/src/lib/errors.js index 47139354f..cb163e9a6 100644 --- a/src/lib/errors.js +++ b/src/lib/errors.js @@ -170,11 +170,7 @@ _.each(statusCodes, function createStatusCodeError(tuple) { var extraData = _.omit(cause, ['type', 'reason']); if (_.size(extraData)) { - memo += ', with { ' + qs.stringify(extraData, ' ', '=', { - encodeURIComponent: function (v) { - return String(v).split('\n').join('\\n'); - } - }) + ' }'; + memo += ', with ' + prettyPrint(extraData); } return memo; @@ -194,3 +190,23 @@ _.each(statusCodes, function createStatusCodeError(tuple) { errors[name] = StatusCodeError; }); }); + + +function prettyPrint(data) { + const path = [] + return (function print(v) { + if (typeof v === 'object') { + if (path.indexOf(v) > -1) return '[circular]' + path.push(v) + try { + return '{ ' + _.map(v, function (subv, name) { + return name + '=' + print(subv) + }).join(' & ') + ' }' + } finally { + path.pop() + } + } else { + return JSON.stringify(v) + } + }(data)) +}