Skip to content

added an optional callback before handling an exception #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ var Raven = {
TK.remoteFetching = true;
}

if (globalOptions.collectWindowErrors) {
TK.collectWindowErrors = true;
}

if (globalOptions.linesOfContext) {
TK.linesOfContext = globalOptions.linesOfContext;
}
Expand All @@ -120,7 +124,7 @@ var Raven = {
install: function() {
if (!isSetup()) return;

TK.report.subscribe(handleStackInfo);
TK.report.subscribe(handleErrorReport);

return Raven;
},
Expand Down Expand Up @@ -182,7 +186,7 @@ var Raven = {
* @return {Raven}
*/
uninstall: function() {
TK.report.unsubscribe(handleStackInfo);
TK.report.unsubscribe(handleErrorReport);

return Raven;
},
Expand Down Expand Up @@ -338,6 +342,24 @@ function getAuthQueryString() {
return cachedAuth;
}

function handleErrorReport(stackInfo, options) {
var cb = globalOptions.shouldReportErrorCallback,
logged = false;

if (cb && cb(stackInfo, options, handleStack)) {
handleStack();
} else if(!cb) {
handleStack();
}

function handleStack() {
if (!logged) {
logged = true;
handleStackInfo(stackInfo, options);
}
}
}

function handleStackInfo(stackInfo, options) {
var frames = [];

Expand Down
69 changes: 67 additions & 2 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,71 @@ describe('globals', function() {
});
});

describe('handleErrorReport', function() {
afterEach(function() {
flushRavenState();
});

it('should pass call handleStackInfo', function() {
var stackInfo = {},
options = {}

setupRaven();
this.sinon.stub(window, 'handleStackInfo')

handleErrorReport()
assert.isTrue(window.handleStackInfo.calledOnce)
})

it('should not call handleStackInfo if cb returns false', function() {
var cb = this.sinon.stub().returns(false)
this.sinon.stub(window, 'handleStackInfo')
Raven.config(SENTRY_DSN, {
shouldReportErrorCallback: cb
})
handleErrorReport()
assert.isTrue(cb.calledOnce)
assert.equal(window.handleStackInfo.callCount, 0)
})

it('should call handleStackInfo if cb returns true', function() {
var cb = this.sinon.stub().returns(true)
this.sinon.stub(window, 'handleStackInfo')
Raven.config(SENTRY_DSN, {
shouldReportErrorCallback: cb
})
handleErrorReport()
assert.isTrue(cb.calledOnce)
assert.isTrue(window.handleStackInfo.calledOnce)
})

it('should allow cb call handleStackInfo through a callback', function() {
var cb = function(stackInfo, options, done) {
done()
}
this.sinon.stub(window, 'handleStackInfo')
Raven.config(SENTRY_DSN, {
shouldReportErrorCallback: cb
})
handleErrorReport()
assert.isTrue(window.handleStackInfo.calledOnce)
})

it('handleStackInfo should only be calledOnce per error', function() {
var cb = function(stackInfo, options, done) {
done()
done()
return true
}
this.sinon.stub(window, 'handleStackInfo')
Raven.config(SENTRY_DSN, {
shouldReportErrorCallback: cb
})
handleErrorReport()
assert.equal(window.handleStackInfo.callCount, 1)
})
})

describe('Raven (public API)', function() {
afterEach(function() {
flushRavenState();
Expand Down Expand Up @@ -868,7 +933,7 @@ describe('Raven (public API)', function() {
this.sinon.stub(TK.report, 'subscribe');
assert.equal(Raven, Raven.install());
assert.isTrue(TK.report.subscribe.calledOnce);
assert.equal(TK.report.subscribe.lastCall.args[0], handleStackInfo);
assert.equal(TK.report.subscribe.lastCall.args[0], handleErrorReport);
});
});

Expand Down Expand Up @@ -963,7 +1028,7 @@ describe('Raven (public API)', function() {
this.sinon.stub(TK.report, 'unsubscribe');
Raven.uninstall();
assert.isTrue(TK.report.unsubscribe.calledOnce);
assert.equal(TK.report.unsubscribe.lastCall.args[0], handleStackInfo);
assert.equal(TK.report.unsubscribe.lastCall.args[0], handleErrorReport);
});
});

Expand Down