Skip to content

Commit

Permalink
leveraged the error formatter when data is an Error
Browse files Browse the repository at this point in the history
for events that are not handled specifically by eventName, the error formatter is used to provide more useful information when the event data is an Error
  • Loading branch information
travi committed Feb 3, 2017
1 parent f0f83c9 commit adef550
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Expand Up @@ -125,6 +125,12 @@ internals.utility = {

formatDefault(event, tags, settings) {

if (event.data instanceof Error) {
const error = event.data;

return internals.utility.formatError(Object.assign(event, { error }), tags, settings);
}

const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data;
const output = `data: ${data}`;

Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Expand Up @@ -460,6 +460,28 @@ describe('GoodConsole', () => {
done();
});
});

it('returns a formatted string for "default" events with data as Error', { plan: 2 }, (done) => {

const reporter = new GoodConsole();
const out = new Streams.Writer();
const reader = new Streams.Reader();

reader.pipe(reporter).pipe(out);

const defaultEvent = Object.assign({}, internals.default);
defaultEvent.data = new Error('you logged an error');

reader.push(defaultEvent);
reader.push(null);

reader.once('end', () => {

expect(out.data).to.have.length(1);
expect(out.data[0].split('\n')[0]).to.be.equal('160318/013330.957, [request,user,info] message: you logged an error stack: Error: you logged an error');
done();
});
});
});
});
});

0 comments on commit adef550

Please sign in to comment.