Skip to content

Commit dbec003

Browse files
authored
Cleaning up wrap, adding interceptErr (#225)
* Cleaning up wrap, adding intercept * Make intercept preserve function details like wrap * Add intercept tests * Change intercept to interceptErr
1 parent bbfa0c0 commit dbec003

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

lib/client.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,39 @@ extend(Raven.prototype, {
241241
};
242242
}
243243

244-
wrapDomain.on('error', function (err) {
245-
onErr(err, wrapDomain.sentryContext);
246-
});
244+
wrapDomain.on('error', onErr);
245+
var wrapped = wrapDomain.bind(func);
246+
247+
for (var property in func) {
248+
if ({}.hasOwnProperty.call(func, property)) {
249+
wrapped[property] = func[property];
250+
}
251+
}
252+
wrapped.prototype = func.prototype;
253+
wrapped.__raven__ = true;
254+
wrapped.__inner__ = func;
255+
// note: domain.bind sets wrapped.domain, but it's not documented, unsure if we should rely on that
256+
wrapped.__domain__ = wrapDomain;
247257

248-
var wrapped = function wrapped() {
249-
// todo make sure this is the best/right way to do this
250-
var args = Array.prototype.slice.call(arguments);
251-
args.unshift(func);
252-
wrapDomain.run.apply(wrapDomain, args);
258+
return wrapped;
259+
},
260+
261+
interceptErr: function (options, func) {
262+
if (!func && typeof options === 'function') {
263+
func = options;
264+
options = {};
265+
}
266+
var self = this;
267+
var wrapped = function () {
268+
var err = arguments[0];
269+
if (err instanceof Error) {
270+
self.captureException(err, options);
271+
} else {
272+
func.apply(null, arguments);
273+
}
253274
};
254275

276+
// repetitive with wrap
255277
for (var property in func) {
256278
if ({}.hasOwnProperty.call(func, property)) {
257279
wrapped[property] = func[property];
@@ -260,7 +282,6 @@ extend(Raven.prototype, {
260282
wrapped.prototype = func.prototype;
261283
wrapped.__raven__ = true;
262284
wrapped.__inner__ = func;
263-
wrapped.__domain__ = wrapDomain;
264285

265286
return wrapped;
266287
},
@@ -282,7 +303,7 @@ extend(Raven.prototype, {
282303
}
283304
},
284305

285-
getContext: function setContext(ctx) {
306+
getContext: function getContext() {
286307
if (!domain.active) {
287308
utils.consoleAlert('attempt to getContext outside context scope');
288309
return null;

test/raven.client.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,61 @@ describe('raven.Client', function () {
836836
client._globalContext.tags.should.have.property('platform', 'OS X');
837837
});
838838
});
839+
840+
describe('#intercept()', function () {
841+
it('should catch an err param', function (done) {
842+
var scope = nock('https://app.getsentry.com')
843+
.filteringRequestBody(/.*/, '*')
844+
.post('/api/269/store/', '*')
845+
.reply(200, function (uri, body) {
846+
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
847+
if (err) return done(err);
848+
var msg = JSON.parse(dec.toString());
849+
msg.message.indexOf('foo').should.not.equal(-1);
850+
done();
851+
});
852+
return 'OK';
853+
});
854+
855+
client.on('logged', function () {
856+
scope.done();
857+
});
858+
859+
client.interceptErr(function (err) {
860+
done(new Error('called wrapped function'));
861+
})(new Error('foo'));
862+
});
863+
864+
it('should pass options to captureException', function (done) {
865+
var scope = nock('https://app.getsentry.com')
866+
.filteringRequestBody(/.*/, '*')
867+
.post('/api/269/store/', '*')
868+
.reply(200, function (uri, body) {
869+
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
870+
if (err) return done(err);
871+
var msg = JSON.parse(dec.toString());
872+
msg.message.indexOf('foo').should.not.equal(-1);
873+
msg.extra.foo.should.equal('bar');
874+
done();
875+
});
876+
return 'OK';
877+
});
878+
879+
client.on('logged', function () {
880+
scope.done();
881+
});
882+
883+
client.interceptErr({ extra: { foo: 'bar' } }, function (err) {
884+
done(new Error('called wrapped function'));
885+
})(new Error('foo'));
886+
});
887+
888+
it('should call original when no err', function (done) {
889+
client.interceptErr(function (err, result) {
890+
if (err != null) return done(err);
891+
result.should.equal('result');
892+
done();
893+
})(null, 'result');
894+
});
895+
});
839896
});

0 commit comments

Comments
 (0)