Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Check for error instances #20

Merged
merged 2 commits into from

2 participants

@danmilon

Picking up from the discussion at #6 (took me some time!)

Checks if attrs.error is an Error instance, and replaces it with the stacktrace as a string or the message if there is no stack attached.

Also there's a test case.

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' });
@tj Owner
tj added a note

might be nice to also support log.error('request', err) directly, expanding to { message: m, stack: s }, both would be nice then you could just --type error --map message and get a nice list

How would --type error work with log.error('request', err)? (type equals request)

@tj Owner
tj added a note

oops sorry --level error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@danmilon

And you can do -s error --map error.message

@tj tj merged commit 0de0f72 into tj:master
@danmilon danmilon referenced this pull request
Closed

handling Error instances #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 14, 2012
  1. Check for error instances

    Dan Milon authored
Commits on Nov 15, 2012
  1. attrs can be just an Error

    Dan Milon authored
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 2 deletions.
  1. +10 −1 lib/jog.js
  2. +17 −1 test/jog.js
View
11 lib/jog.js
@@ -99,6 +99,15 @@ Jog.prototype.write = function(level, type, attrs){
});
}
+ // check for Error
+ var err = attrs.error || attrs
+ if (err instanceof Error) {
+ attrs.error = {
+ stack: err.stack,
+ message: err.message
+ };
+ }
+
// add it to the store
this.store.add(attrs);
return this;
@@ -137,4 +146,4 @@ exports.levels.forEach(function(level){
Jog.prototype[level] = function(type, attrs){
this.write(level, type, attrs);
};
-})
+})
View
18 test/jog.js
@@ -30,6 +30,22 @@ 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.stack.should.equal(err.stack);
+ obj.error.message.should.equal(err.message);
+ }
+ };
+
+ var log = new Jog(store);
+ log.error('something happened', { error: err });
+ log.error('even worse', err);
+ done();
+ })
})
describe('#ns(obj)', function(){
@@ -52,4 +68,4 @@ describe('Jog', function(){
log.info('something happened');
})
})
-})
+})
Something went wrong with that request. Please try again.