Skip to content

Commit

Permalink
Cleaning up wrap, adding interceptErr (#225)
Browse files Browse the repository at this point in the history
* Cleaning up wrap, adding intercept

* Make intercept preserve function details like wrap

* Add intercept tests

* Change intercept to interceptErr
  • Loading branch information
LewisJEllis committed Nov 1, 2016
1 parent bbfa0c0 commit dbec003
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
41 changes: 31 additions & 10 deletions lib/client.js
Expand Up @@ -241,17 +241,39 @@ extend(Raven.prototype, {
};
}

wrapDomain.on('error', function (err) {
onErr(err, wrapDomain.sentryContext);
});
wrapDomain.on('error', onErr);
var wrapped = wrapDomain.bind(func);

for (var property in func) {
if ({}.hasOwnProperty.call(func, property)) {
wrapped[property] = func[property];
}
}
wrapped.prototype = func.prototype;
wrapped.__raven__ = true;
wrapped.__inner__ = func;
// note: domain.bind sets wrapped.domain, but it's not documented, unsure if we should rely on that
wrapped.__domain__ = wrapDomain;

var wrapped = function wrapped() {
// todo make sure this is the best/right way to do this
var args = Array.prototype.slice.call(arguments);
args.unshift(func);
wrapDomain.run.apply(wrapDomain, args);
return wrapped;
},

interceptErr: function (options, func) {
if (!func && typeof options === 'function') {
func = options;
options = {};
}
var self = this;
var wrapped = function () {
var err = arguments[0];
if (err instanceof Error) {
self.captureException(err, options);
} else {
func.apply(null, arguments);
}
};

// repetitive with wrap
for (var property in func) {
if ({}.hasOwnProperty.call(func, property)) {
wrapped[property] = func[property];
Expand All @@ -260,7 +282,6 @@ extend(Raven.prototype, {
wrapped.prototype = func.prototype;
wrapped.__raven__ = true;
wrapped.__inner__ = func;
wrapped.__domain__ = wrapDomain;

return wrapped;
},
Expand All @@ -282,7 +303,7 @@ extend(Raven.prototype, {
}
},

getContext: function setContext(ctx) {
getContext: function getContext() {
if (!domain.active) {
utils.consoleAlert('attempt to getContext outside context scope');
return null;
Expand Down
57 changes: 57 additions & 0 deletions test/raven.client.js
Expand Up @@ -836,4 +836,61 @@ describe('raven.Client', function () {
client._globalContext.tags.should.have.property('platform', 'OS X');
});
});

describe('#intercept()', function () {
it('should catch an err param', function (done) {
var scope = nock('https://app.getsentry.com')
.filteringRequestBody(/.*/, '*')
.post('/api/269/store/', '*')
.reply(200, function (uri, body) {
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
if (err) return done(err);
var msg = JSON.parse(dec.toString());
msg.message.indexOf('foo').should.not.equal(-1);
done();
});
return 'OK';
});

client.on('logged', function () {
scope.done();
});

client.interceptErr(function (err) {
done(new Error('called wrapped function'));
})(new Error('foo'));
});

it('should pass options to captureException', function (done) {
var scope = nock('https://app.getsentry.com')
.filteringRequestBody(/.*/, '*')
.post('/api/269/store/', '*')
.reply(200, function (uri, body) {
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
if (err) return done(err);
var msg = JSON.parse(dec.toString());
msg.message.indexOf('foo').should.not.equal(-1);
msg.extra.foo.should.equal('bar');
done();
});
return 'OK';
});

client.on('logged', function () {
scope.done();
});

client.interceptErr({ extra: { foo: 'bar' } }, function (err) {
done(new Error('called wrapped function'));
})(new Error('foo'));
});

it('should call original when no err', function (done) {
client.interceptErr(function (err, result) {
if (err != null) return done(err);
result.should.equal('result');
done();
})(null, 'result');
});
});
});

0 comments on commit dbec003

Please sign in to comment.