Skip to content

Commit 5db4716

Browse files
committed
includePaths, fixes #72
1 parent fcca2d7 commit 5db4716

File tree

3 files changed

+89
-12
lines changed

3 files changed

+89
-12
lines changed

docs/config/index.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ Similar to ``ignoreErrors``, but will ignore errors from whole urls patching a r
5555
ignoreUrls: [/graph\.facebook\.com/i]
5656
}
5757
58+
includePaths
59+
------------
60+
61+
An array of regex patterns to indicate which urls are a part of your app. All other frames will appear collapsed inside Sentry to make it easier to discern between frames that happened in your code vs other code. It'd be suggested to add the current page url, and the host for your CDN.
62+
63+
.. code-block:: javascript
64+
65+
{
66+
includePaths: [/https?:\/\/getsentry\.com/, /https?:\/\/cdn\.getsentry\.com/]
67+
}
68+
5869
Putting it all together
5970
~~~~~~~~~~~~~~~~~~~~~~~
6071

@@ -73,6 +84,10 @@ Putting it all together
7384
],
7485
ignoreErrors: [
7586
'fb_xd_fragment'
87+
],
88+
includePaths: [
89+
/https?:\/\/(www\.)?getsentry\.com/,
90+
/https?:\/\/d3nslu0hdya83q\.cloudfront\.net/
7691
]
7792
};
7893
Raven.config('https://[email protected]/1', options).install();

src/raven.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var _Raven = window.Raven,
1313
logger: 'javascript',
1414
ignoreErrors: [],
1515
ignoreUrls: [],
16+
includePaths: [],
1617
tags: {}
1718
};
1819

@@ -59,6 +60,10 @@ var Raven = {
5960
});
6061
}
6162

63+
// join regexp rules into one big rule
64+
globalOptions.ignoreUrls = globalOptions.ignoreUrls.length ? joinRegExp(globalOptions.ignoreUrls) : false;
65+
globalOptions.includePaths = joinRegExp(globalOptions.includePaths);
66+
6267
// "Script error." is hard coded into browsers for errors that it can't read.
6368
// this is the result of a script being pulled in from an external domain and CORS.
6469
globalOptions.ignoreErrors.push('Script error.');
@@ -301,14 +306,22 @@ function normalizeFrame(frame) {
301306
lineno: frame.line,
302307
colno: frame.column,
303308
'function': frame.func || '?'
304-
}, context = extractContextFromFrame(frame);
309+
}, context = extractContextFromFrame(frame), i;
305310

306311
if (context) {
307-
var i = 3, keys = ['pre_context', 'context_line', 'post_context'];
312+
var keys = ['pre_context', 'context_line', 'post_context'];
313+
i = 3;
308314
while (i--) normalized[keys[i]] = context[i];
309315
}
310316

311-
// normalized.in_app = !/(Raven|TraceKit)\./.test(normalized['function']);
317+
normalized.in_app = !( // determine if an exception came from outside of our app
318+
// first we check the global includePaths list.
319+
!globalOptions.includePaths.test(normalized.filename) ||
320+
// Now we check for fun, if the function name is Raven or TraceKit
321+
/(Raven|TraceKit)\./.test(normalized['function']) ||
322+
// finally, we do a last ditch effort and check for raven.min.js
323+
/raven\.(min\.)js$/.test(normalized.filename)
324+
);
312325

313326
return normalized;
314327
}
@@ -376,12 +389,7 @@ function processException(type, message, fileurl, lineno, frames, options) {
376389
};
377390
}
378391

379-
i = globalOptions.ignoreUrls.length;
380-
while (i--) {
381-
if (globalOptions.ignoreUrls[i].test(fileurl)) {
382-
return;
383-
}
384-
}
392+
if (globalOptions.ignoreUrls && globalOptions.ignoreUrls.test(fileurl)) return;
385393

386394
label = lineno ? message + ' at ' + lineno : message;
387395

@@ -480,3 +488,11 @@ function wrapArguments(what) {
480488
for (var k in what) wrapped[k] = what[k];
481489
return wrapped;
482490
}
491+
492+
function joinRegExp(patterns) {
493+
// Combine an array of regular expressions into one large regexp
494+
var sources = [], i = patterns.length;
495+
// lol, map
496+
while (i--) sources[i] = patterns[i].source;
497+
return new RegExp(sources.join('|'), 'i');
498+
}

test/raven.test.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function flushRavenState() {
88
logger: 'javascript',
99
ignoreErrors: [],
1010
ignoreUrls: [],
11+
includePaths: [],
1112
tags: {}
1213
};
1314
Raven.uninstall();
@@ -32,6 +33,7 @@ function setupRaven() {
3233

3334
describe('globals', function() {
3435
beforeEach(function() {
36+
setupRaven();
3537
globalOptions.fetchContext = true;
3638
});
3739

@@ -160,7 +162,7 @@ describe('globals', function() {
160162
pre_context: ['line1'],
161163
context_line: 'line2',
162164
post_context: ['line3'],
163-
// in_app: true
165+
in_app: true
164166
});
165167
});
166168

@@ -181,7 +183,51 @@ describe('globals', function() {
181183
lineno: 10,
182184
colno: 11,
183185
'function': 'lol',
184-
// in_app: true
186+
in_app: true
187+
});
188+
});
189+
190+
it('should not mark `in_app` if rules match', function() {
191+
this.sinon.stub(window, 'extractContextFromFrame').returns(undefined);
192+
var frame = {
193+
url: 'http://example.com/path/file.js',
194+
line: 10,
195+
column: 11,
196+
func: 'lol'
197+
// context: [] context is stubbed
198+
};
199+
200+
globalOptions.fetchContext = true;
201+
globalOptions.includePaths = /^http:\/\/example\.com/;
202+
203+
assert.deepEqual(normalizeFrame(frame), {
204+
filename: 'http://example.com/path/file.js',
205+
lineno: 10,
206+
colno: 11,
207+
'function': 'lol',
208+
in_app: true
209+
});
210+
});
211+
212+
it('should mark `in_app` if rules do not match', function() {
213+
this.sinon.stub(window, 'extractContextFromFrame').returns(undefined);
214+
var frame = {
215+
url: 'http://lol.com/path/file.js',
216+
line: 10,
217+
column: 11,
218+
func: 'lol'
219+
// context: [] context is stubbed
220+
};
221+
222+
globalOptions.fetchContext = true;
223+
globalOptions.includePaths = /^http:\/\/example\.com/;
224+
225+
assert.deepEqual(normalizeFrame(frame), {
226+
filename: 'http://lol.com/path/file.js',
227+
lineno: 10,
228+
colno: 11,
229+
'function': 'lol',
230+
in_app: false
185231
});
186232
});
187233
});
@@ -278,7 +324,7 @@ describe('globals', function() {
278324
it('should respect `ignoreUrls`', function() {
279325
this.sinon.stub(window, 'send');
280326

281-
globalOptions.ignoreUrls = [/.+?host1.+/, /.+?host2.+/];
327+
globalOptions.ignoreUrls = joinRegExp([/.+?host1.+/, /.+?host2.+/]);
282328
processException('Error', 'error', 'http://host1/', []);
283329
assert.isFalse(window.send.called);
284330
processException('Error', 'error', 'http://host2/', []);

0 commit comments

Comments
 (0)