Skip to content

Commit 6aff89c

Browse files
committed
Merge pull request #515 from nevir/rn-prefix
Add a `pathStrip` option to the react native plugin
2 parents 616eba7 + 85d122a commit 6aff89c

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

plugins/react-native.js

+31-17
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
*
66
* Usage:
77
* var Raven = require('raven-js');
8-
* require('raven-js/plugins/react-native')(Raven);
8+
* Raven.addPlugin(require('raven-js/plugins/react-native'));
9+
*
10+
* Options:
11+
*
12+
* pathStrip: A RegExp that matches the portions of a file URI that should be
13+
* removed from stacks prior to submission.
14+
*
915
*/
1016
'use strict';
1117

12-
var DEVICE_PATH_RE = /^\/var\/mobile\/Containers\/Bundle\/Application\/[^\/]+\/[^\.]+\.app/;
18+
var PATH_STRIP_RE = /^\/var\/mobile\/Containers\/Bundle\/Application\/[^\/]+\/[^\.]+\.app/;
1319

1420
/**
1521
* Strip device-specific IDs from React Native file:// paths
1622
*/
17-
function normalizeUrl(url) {
23+
function normalizeUrl(url, pathStripRe) {
1824
return url
1925
.replace(/^file\:\/\//, '')
20-
.replace(DEVICE_PATH_RE, '');
26+
.replace(pathStripRe, '');
2127
}
2228

2329
/**
@@ -27,29 +33,33 @@ function normalizeUrl(url) {
2733
function urlencode(obj) {
2834
var pairs = [];
2935
for (var key in obj) {
30-
if ({}.hasOwnProperty.call(obj, key))
31-
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
36+
if ({}.hasOwnProperty.call(obj, key))
37+
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
3238
}
3339
return pairs.join('&');
3440
}
3541

3642
/**
3743
* Initializes React Native plugin
3844
*/
39-
function reactNativePlugin(Raven) {
45+
function reactNativePlugin(Raven, options) {
46+
options = options || {};
47+
4048
// react-native doesn't have a document, so can't use default Image
4149
// transport - use XMLHttpRequest instead
4250
Raven.setTransport(reactNativePlugin._transport);
4351

4452
// Use data callback to strip device-specific paths from stack traces
45-
Raven.setDataCallback(reactNativePlugin._normalizeData);
53+
Raven.setDataCallback(function(data) {
54+
reactNativePlugin._normalizeData(data, options.pathStrip)
55+
});
4656

4757
var defaultHandler = ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler() || ErrorUtils._globalHandler;
4858

4959
ErrorUtils.setGlobalHandler(function() {
50-
var error = arguments[0];
51-
defaultHandler.apply(this, arguments)
52-
Raven.captureException(error);
60+
var error = arguments[0];
61+
defaultHandler.apply(this, arguments)
62+
Raven.captureException(error);
5363
});
5464
}
5565

@@ -92,16 +102,20 @@ reactNativePlugin._transport = function (options) {
92102
* Strip device-specific IDs found in culprit and frame filenames
93103
* when running React Native applications on a physical device.
94104
*/
95-
reactNativePlugin._normalizeData = function (data) {
105+
reactNativePlugin._normalizeData = function (data, pathStripRe) {
106+
if (!pathStripRe) {
107+
pathStripRe = PATH_STRIP_RE;
108+
}
109+
96110
if (data.culprit) {
97-
data.culprit = normalizeUrl(data.culprit);
111+
data.culprit = normalizeUrl(data.culprit, pathStripRe);
98112
}
99113

100114
if (data.exception) {
101-
// if data.exception exists, all of the other keys are guaranteed to exist
102-
data.exception.values[0].stacktrace.frames.forEach(function (frame) {
103-
frame.filename = normalizeUrl(frame.filename);
104-
});
115+
// if data.exception exists, all of the other keys are guaranteed to exist
116+
data.exception.values[0].stacktrace.frames.forEach(function (frame) {
117+
frame.filename = normalizeUrl(frame.filename, pathStripRe);
118+
});
105119
}
106120
};
107121

0 commit comments

Comments
 (0)