Skip to content

Commit

Permalink
logging message rather than throwing (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonwinton authored May 4, 2018
1 parent ce48e2e commit 70311f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 9 additions & 4 deletions lib/services/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const _ = require('lodash'),
references = require('./references'),
mapLayoutToPageData = require('../utils/layout-to-page-data'),
referenceProperty = '_ref';
var log = require('./logger').setup({ file: __filename });

/**
* Compose a component, recursively filling in all component references with
Expand All @@ -26,11 +27,12 @@ function resolveComponentReferences(data, locals, filter = referenceProperty) {
return resolveComponentReferences(obj, locals, filter).finally(function () {
_.assign(referenceObject, _.omit(obj, referenceProperty));
}).catch(function (error) {
// add additional information to the error message
const wrappedError = new Error(error.message + ' within ' + referenceObject[referenceProperty]);
log('error', `${error.message} within ${referenceObject[referenceProperty]}`, {
name: error.name,
stack: error.stack
});

wrappedError.name = error.name;
throw wrappedError;
return bluebird.reject(error);
});
});
}).return(data);
Expand All @@ -55,3 +57,6 @@ function composePage(pageData, locals) {

module.exports.resolveComponentReferences = resolveComponentReferences;
module.exports.composePage = composePage;

// For testing
module.exports.setLog = (fakeLogger) => log = fakeLogger;
11 changes: 7 additions & 4 deletions lib/services/composer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ const _ = require('lodash'),
sinon = require('sinon');

describe(_.startCase(filename), function () {
let sandbox;
let sandbox, logSpy;

beforeEach(function () {
sandbox = sinon.sandbox.create();
logSpy = sandbox.spy();

sandbox.stub(components);
lib.setLog(logSpy);
});

afterEach(function () {
Expand Down Expand Up @@ -104,9 +107,9 @@ describe(_.startCase(filename), function () {
components.get.withArgs('/c/m').returns(bluebird.reject(myError));

// use done() rather than returning the promise, so we can catch and test errors
fn(data).then(done).catch(function (error) {
expect(error.message).to.equal('hello! within /c/e');
expect(error.message).to.match(/hello! within \/c\/e/);
fn(data).then(done).catch((error) => {
sinon.assert.calledOnce(logSpy);
expect(error.message).to.equal('hello!');
done();
});
});
Expand Down

0 comments on commit 70311f2

Please sign in to comment.