Skip to content
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
40 changes: 21 additions & 19 deletions plugins/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
* pathStrip: A RegExp that matches the portions of a file URI that should be
* removed from stacks prior to submission.
*
* normalizeUrl: A function that is given the file:// URI to a bundle, and
* should return its normalized form (used for release asset lookup).
*
*/
'use strict';

var PATH_STRIP_RE = /^\/var\/mobile\/Containers\/Bundle\/Application\/[^\/]+\/[^\.]+\.app/;

/**
* Strip device-specific IDs from React Native file:// paths
*/
function normalizeUrl(url, pathStripRe) {
return url
.replace(/^file\:\/\//, '')
.replace(pathStripRe, '');
}
var DEFAULT_PATH_STRIP_RE = /^\/var\/mobile\/Containers\/Bundle\/Application\/[^\/]+\/[^\.]+\.app/;

/**
* Extract key/value pairs from an object and encode them for
Expand All @@ -44,21 +38,33 @@ function urlencode(obj) {
*/
function reactNativePlugin(Raven, options) {
options = options || {};
if (options.pathStrip && options.normalizeUrl) {
throw new TypeError('Please pass either pathStrip or normalizeUrl, not both');
}
var normalizeUrl = options.normalizeUrl;
if (!normalizeUrl) {
var pathStripRe = options.pathStripRe || DEFAULT_PATH_STRIP_RE;
normalizeUrl = function defaultNormalizeUrl(url) {
return url
.replace(/^file\:\/\//, '')
.replace(pathStripRe, '');
};
}

// react-native doesn't have a document, so can't use default Image
// transport - use XMLHttpRequest instead
Raven.setTransport(reactNativePlugin._transport);

// Use data callback to strip device-specific paths from stack traces
Raven.setDataCallback(function(data) {
reactNativePlugin._normalizeData(data, options.pathStrip)
reactNativePlugin._normalizeData(data, normalizeUrl);
});

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

ErrorUtils.setGlobalHandler(function() {
var error = arguments[0];
defaultHandler.apply(this, arguments)
defaultHandler.apply(this, arguments);
Raven.captureException(error);
});
}
Expand Down Expand Up @@ -102,19 +108,15 @@ reactNativePlugin._transport = function (options) {
* Strip device-specific IDs found in culprit and frame filenames
* when running React Native applications on a physical device.
*/
reactNativePlugin._normalizeData = function (data, pathStripRe) {
if (!pathStripRe) {
pathStripRe = PATH_STRIP_RE;
}

reactNativePlugin._normalizeData = function (data, normalizeUrl) {
if (data.culprit) {
data.culprit = normalizeUrl(data.culprit, pathStripRe);
data.culprit = normalizeUrl(data.culprit);
}

if (data.exception) {
// if data.exception exists, all of the other keys are guaranteed to exist
data.exception.values[0].stacktrace.frames.forEach(function (frame) {
frame.filename = normalizeUrl(frame.filename, pathStripRe);
frame.filename = normalizeUrl(frame.filename);
});
}
};
Expand Down
10 changes: 6 additions & 4 deletions test/plugins/react-native.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ describe('React Native plugin', function () {
}],
}
};
reactNativePlugin._normalizeData(data);
reactNativePlugin._normalizeData(data, function(uri) {
return uri.replace(/^.*\//, '');
});

assert.equal(data.culprit, '/app.js');
assert.equal(data.culprit, 'app.js');
var frames = data.exception.values[0].stacktrace.frames;
assert.equal(frames[0].filename, '/file1.js');
assert.equal(frames[1].filename, '/file2.js');
assert.equal(frames[0].filename, 'file1.js');
assert.equal(frames[1].filename, 'file2.js');
});
});

Expand Down