Skip to content

Commit d7beae8

Browse files
HeroProtagonistkamilogorek
authored andcommitted
feat: Make captureMessage respects ignoreUrls/whitelistUrls
1 parent b81a3f6 commit d7beae8

File tree

3 files changed

+94
-25
lines changed

3 files changed

+94
-25
lines changed

docs/config.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ Those configuration options are documented below:
9090
tags. Not setting this value is equivalent to a catch-all and will not
9191
filter out any values.
9292

93-
Does not affect ``captureMessage`` or when non-error object is passed in
94-
as argument to captureException.
95-
9693
.. code-block:: javascript
9794
9895
{
@@ -107,9 +104,6 @@ Those configuration options are documented below:
107104
messages to be filtered out before being sent to Sentry as either
108105
regular expressions or strings.
109106

110-
Does not affect captureMessage or when non-error object is passed in
111-
as argument to captureException.
112-
113107
.. code-block:: javascript
114108
115109
{
@@ -128,9 +122,6 @@ Those configuration options are documented below:
128122
ignoreUrls: [/graph\.facebook\.com/, 'http://example.com/script2.js']
129123
}
130124
131-
Does not affect captureMessage or when non-error object is passed in
132-
as argument to ``captureException``.
133-
134125
.. describe:: includePaths
135126

136127
An array of regex patterns to indicate which urls are a part of your

src/raven.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -435,21 +435,41 @@ Raven.prototype = {
435435
options
436436
);
437437

438-
if (this._globalOptions.stacktrace || (options && options.stacktrace)) {
439-
var ex;
440-
// Generate a "synthetic" stack trace from this point.
441-
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it is NOT indicative
442-
// of a bug with Raven.js. Sentry generates synthetic traces either by configuration,
443-
// or if it catches a thrown object without a "stack" property.
444-
try {
445-
throw new Error(msg);
446-
} catch (ex1) {
447-
ex = ex1;
448-
}
438+
var ex;
439+
// Generate a "synthetic" stack trace from this point.
440+
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it is NOT indicative
441+
// of a bug with Raven.js. Sentry generates synthetic traces either by configuration,
442+
// or if it catches a thrown object without a "stack" property.
443+
try {
444+
throw new Error(msg);
445+
} catch (ex1) {
446+
ex = ex1;
447+
}
448+
449+
// null exception name so `Error` isn't prefixed to msg
450+
ex.name = null;
451+
var stack = TraceKit.computeStackTrace(ex);
452+
453+
// stack[0] is `throw new Error(msg)` call itself, we are interested in the frame that was just before that, stack[1]
454+
var initialCall = stack.stack[1];
449455

450-
// null exception name so `Error` isn't prefixed to msg
451-
ex.name = null;
456+
var fileurl = initialCall.url || '';
452457

458+
if (
459+
!!this._globalOptions.ignoreUrls.test &&
460+
this._globalOptions.ignoreUrls.test(fileurl)
461+
) {
462+
return;
463+
}
464+
465+
if (
466+
!!this._globalOptions.whitelistUrls.test &&
467+
!this._globalOptions.whitelistUrls.test(fileurl)
468+
) {
469+
return;
470+
}
471+
472+
if (this._globalOptions.stacktrace || (options && options.stacktrace)) {
453473
options = objectMerge(
454474
{
455475
// fingerprint on msg, not stack trace (legacy behavior, could be
@@ -463,7 +483,6 @@ Raven.prototype = {
463483
options
464484
);
465485

466-
var stack = TraceKit.computeStackTrace(ex);
467486
var frames = this._prepareFrames(stack, options);
468487
data.stacktrace = {
469488
// Sentry expects frames oldest to newest
@@ -1452,13 +1471,16 @@ Raven.prototype = {
14521471
if (
14531472
!!this._globalOptions.ignoreUrls.test &&
14541473
this._globalOptions.ignoreUrls.test(fileurl)
1455-
)
1474+
) {
14561475
return;
1476+
}
1477+
14571478
if (
14581479
!!this._globalOptions.whitelistUrls.test &&
14591480
!this._globalOptions.whitelistUrls.test(fileurl)
1460-
)
1481+
) {
14611482
return;
1483+
}
14621484

14631485
var data = objectMerge(
14641486
{

test/raven.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,62 @@ describe('Raven (public API)', function() {
26572657
});
26582658
});
26592659

2660+
it('should respect `ignoreUrls`', function() {
2661+
this.sinon.stub(Raven, '_send');
2662+
var TraceKitStub = this.sinon.stub(TraceKit, 'computeStackTrace');
2663+
2664+
Raven._globalOptions.ignoreUrls = joinRegExp([/.+?host1.+/, /.+?host2.+/]);
2665+
TraceKitStub.returns({stack: [{url: 'http://host1/'}, {url: 'http://host1/'}]});
2666+
Raven.captureMessage('Do not capture');
2667+
assert.isFalse(Raven._send.called);
2668+
TraceKitStub.returns({stack: [{url: 'http://host2/'}, {url: 'http://host2/'}]});
2669+
Raven.captureMessage('Also do not capture');
2670+
assert.isFalse(Raven._send.called);
2671+
TraceKitStub.returns({stack: [{url: 'http://host3/'}, {url: 'http://host3/'}]});
2672+
Raven.captureMessage('Capture!');
2673+
assert.isTrue(Raven._send.calledOnce);
2674+
});
2675+
2676+
it('should handle empty `ignoreUrls`', function() {
2677+
this.sinon.stub(Raven, '_send');
2678+
var TraceKitStub = this.sinon.stub(TraceKit, 'computeStackTrace');
2679+
2680+
Raven._globalOptions.ignoreUrls = [];
2681+
TraceKitStub.returns({
2682+
stack: [{url: 'http://example.com'}, {url: 'http://example.com'}]
2683+
});
2684+
Raven.captureMessage('Capture!');
2685+
assert.isTrue(Raven._send.calledOnce);
2686+
});
2687+
2688+
it('should respect `whitelistUrls`', function() {
2689+
this.sinon.stub(Raven, '_send');
2690+
var TraceKitStub = this.sinon.stub(TraceKit, 'computeStackTrace');
2691+
2692+
Raven._globalOptions.whitelistUrls = joinRegExp([/.+?host1.+/, /.+?host2.+/]);
2693+
TraceKitStub.returns({stack: [{url: 'http://host1/'}, {url: 'http://host1/'}]});
2694+
Raven.captureMessage('Capture!');
2695+
assert.isTrue(Raven._send.calledOnce);
2696+
TraceKitStub.returns({stack: [{url: 'http://host2/'}, {url: 'http://host2/'}]});
2697+
Raven.captureMessage('Also capture!');
2698+
assert.isTrue(Raven._send.calledTwice);
2699+
TraceKitStub.returns({stack: [{url: 'http://host3/'}, {url: 'http://host3/'}]});
2700+
Raven.captureMessage('Do not capture!');
2701+
assert.isTrue(Raven._send.calledTwice);
2702+
});
2703+
2704+
it('should handle empty `whitelistUrls`', function() {
2705+
this.sinon.stub(Raven, '_send');
2706+
var TraceKitStub = this.sinon.stub(TraceKit, 'computeStackTrace');
2707+
2708+
Raven._globalOptions.whitelistUrls = [];
2709+
TraceKitStub.returns({
2710+
stack: [{url: 'http://example.com'}, {url: 'http://example.com'}]
2711+
});
2712+
Raven.captureMessage('Capture!');
2713+
assert.isTrue(Raven._send.calledOnce);
2714+
});
2715+
26602716
describe('synthetic traces', function() {
26612717
function assertSynthetic(frames) {
26622718
// Raven.captureMessage

0 commit comments

Comments
 (0)