Skip to content

Commit 6ac383a

Browse files
committed
can set maxErrors config option
1 parent b7503d0 commit 6ac383a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/raven.js

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function Raven() {
5757
this._keypressTimeout;
5858
this._location = _window.location;
5959
this._lastHref = this._location && this._location.href;
60+
this._sentErrors = 0;
6061

6162
for (var method in this._originalConsole) { // eslint-disable-line guard-for-in
6263
this._originalConsoleMethods[method] = this._originalConsole[method];
@@ -770,6 +771,9 @@ Raven.prototype = {
770771
var parsedTo = parseUrl(to);
771772
var parsedFrom = parseUrl(from);
772773

774+
//refresh max error count
775+
this._sentErrors = 0;
776+
773777
// because onpopstate only tells you the "new" (to) value of location.href, and
774778
// not the previous (from) value, we need to track the value of the current URL
775779
// state ourselves
@@ -1059,6 +1063,9 @@ Raven.prototype = {
10591063
return function (/* state, title, url */) {
10601064
var url = arguments.length > 2 ? arguments[2] : undefined;
10611065

1066+
//refresh max error count
1067+
self._sentErrors = 0;
1068+
10621069
// url argument is optional
10631070
if (url) {
10641071
// coerce to string (this is what pushState does)
@@ -1352,7 +1359,12 @@ Raven.prototype = {
13521359
if (isFunction(globalOptions.shouldSendCallback) && !globalOptions.shouldSendCallback(data)) {
13531360
return;
13541361
}
1362+
//Check if the request should be filtered due to errors per page
1363+
if (globalOptions.maxErrors && this._sentErrors >= globalOptions.maxErrors){
1364+
return;
1365+
}
13551366

1367+
this._sentErrors++; //failed errors count towards maxErrors
13561368
this._sendProcessedPayload(data);
13571369
},
13581370

test/raven.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,60 @@ describe('Raven (public API)', function() {
22182218
});
22192219
});
22202220

2221+
describe('maxErrors config', function(){
2222+
2223+
it('allows many errors when maxErrors is undefined', function () {
2224+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2225+
this.sinon.spy(stub)
2226+
2227+
Raven.captureException(new Error('foo'))
2228+
Raven.captureException(new Error('foo'))
2229+
Raven.captureException(new Error('foo'))
2230+
Raven.captureException(new Error('foo'))
2231+
Raven.captureException(new Error('foo'))
2232+
2233+
2234+
assert.equal(Raven._sendProcessedPayload.callCount, 5);
2235+
});
2236+
2237+
it('should only allow up to maxErrors requests', function () {
2238+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2239+
this.sinon.spy(stub)
2240+
2241+
Raven._globalOptions.maxErrors = 3;
2242+
2243+
Raven.captureException(new Error('foo'))
2244+
Raven.captureException(new Error('foo'))
2245+
Raven.captureException(new Error('foo'))
2246+
Raven.captureException(new Error('foo'))
2247+
Raven.captureException(new Error('foo'))
2248+
2249+
assert.equal(Raven._sendProcessedPayload.callCount, 3);
2250+
});
2251+
2252+
it('should reset maxErrors on SPA page change', function () {
2253+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2254+
this.sinon.spy(stub)
2255+
2256+
Raven._globalOptions.maxErrors = 3;
2257+
2258+
Raven.captureException(new Error('foo'))
2259+
Raven.captureException(new Error('foo'))
2260+
Raven.captureException(new Error('foo'))
2261+
Raven.captureException(new Error('foo'))
2262+
2263+
assert.equal(Raven._sendProcessedPayload.callCount, 3);
2264+
2265+
Raven._captureUrlChange('/foo', '/bar');
2266+
2267+
Raven.captureException(new Error('foo'))
2268+
Raven.captureException(new Error('foo'))
2269+
2270+
assert.equal(Raven._sendProcessedPayload.callCount, 5);
2271+
});
2272+
2273+
});
2274+
22212275
describe('._captureUrlChange', function () {
22222276
it('should create a new breadcrumb from its "from" and "to" arguments', function () {
22232277
Raven._breadcrumbs = [];

0 commit comments

Comments
 (0)