From d7bb299c30c636f7a9b3f6ad289924fbb59a1412 Mon Sep 17 00:00:00 2001 From: Dan Milon Date: Wed, 14 Nov 2012 03:26:33 +0200 Subject: [PATCH 1/2] Check for error instances --- lib/jog.js | 7 ++++++- test/jog.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/jog.js b/lib/jog.js index cb623ed..cc0d426 100644 --- a/lib/jog.js +++ b/lib/jog.js @@ -99,6 +99,11 @@ Jog.prototype.write = function(level, type, attrs){ }); } + // check for Error + if (attrs.error instanceof Error) { + attrs.error = attrs.error.stack || attrs.error.message; + } + // add it to the store this.store.add(attrs); return this; @@ -137,4 +142,4 @@ exports.levels.forEach(function(level){ Jog.prototype[level] = function(type, attrs){ this.write(level, type, attrs); }; -}) \ No newline at end of file +}) diff --git a/test/jog.js b/test/jog.js index 3a64003..8c0778c 100644 --- a/test/jog.js +++ b/test/jog.js @@ -30,6 +30,21 @@ describe('Jog', function(){ var log = new Jog(store); log.info('something happened'); }) + + it('should handle error instances', function (done){ + var err = new Error('BOOM') + + var store = { + add: function(obj){ + obj.error.should.equal(err.stack); + obj.x.should.equal('y'); + done(); + } + }; + + var log = new Jog(store); + log.error('something happened', { error: err, x: 'y' }); + }) }) describe('#ns(obj)', function(){ @@ -52,4 +67,4 @@ describe('Jog', function(){ log.info('something happened'); }) }) -}) \ No newline at end of file +}) From 0b87fad3e4016480f6260c862f66a60f0bf29b29 Mon Sep 17 00:00:00 2001 From: Dan Milon Date: Thu, 15 Nov 2012 02:21:43 +0200 Subject: [PATCH 2/2] attrs can be just an Error --- lib/jog.js | 8 ++++++-- test/jog.js | 9 +++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/jog.js b/lib/jog.js index cc0d426..b71dbbf 100644 --- a/lib/jog.js +++ b/lib/jog.js @@ -100,8 +100,12 @@ Jog.prototype.write = function(level, type, attrs){ } // check for Error - if (attrs.error instanceof Error) { - attrs.error = attrs.error.stack || attrs.error.message; + var err = attrs.error || attrs + if (err instanceof Error) { + attrs.error = { + stack: err.stack, + message: err.message + }; } // add it to the store diff --git a/test/jog.js b/test/jog.js index 8c0778c..aa0f6dc 100644 --- a/test/jog.js +++ b/test/jog.js @@ -36,14 +36,15 @@ describe('Jog', function(){ var store = { add: function(obj){ - obj.error.should.equal(err.stack); - obj.x.should.equal('y'); - done(); + obj.error.stack.should.equal(err.stack); + obj.error.message.should.equal(err.message); } }; var log = new Jog(store); - log.error('something happened', { error: err, x: 'y' }); + log.error('something happened', { error: err }); + log.error('even worse', err); + done(); }) })