5
5
*
6
6
* Usage:
7
7
* 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
+ *
9
15
*/
10
16
'use strict' ;
11
17
12
- var DEVICE_PATH_RE = / ^ \/ v a r \/ m o b i l e \/ C o n t a i n e r s \/ B u n d l e \/ A p p l i c a t i o n \/ [ ^ \/ ] + \/ [ ^ \. ] + \. a p p / ;
18
+ var PATH_STRIP_RE = / ^ \/ v a r \/ m o b i l e \/ C o n t a i n e r s \/ B u n d l e \/ A p p l i c a t i o n \/ [ ^ \/ ] + \/ [ ^ \. ] + \. a p p / ;
13
19
14
20
/**
15
21
* Strip device-specific IDs from React Native file:// paths
16
22
*/
17
- function normalizeUrl ( url ) {
23
+ function normalizeUrl ( url , pathStripRe ) {
18
24
return url
19
25
. replace ( / ^ f i l e \: \/ \/ / , '' )
20
- . replace ( DEVICE_PATH_RE , '' ) ;
26
+ . replace ( pathStripRe , '' ) ;
21
27
}
22
28
23
29
/**
@@ -27,29 +33,33 @@ function normalizeUrl(url) {
27
33
function urlencode ( obj ) {
28
34
var pairs = [ ] ;
29
35
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 ] ) ) ;
32
38
}
33
39
return pairs . join ( '&' ) ;
34
40
}
35
41
36
42
/**
37
43
* Initializes React Native plugin
38
44
*/
39
- function reactNativePlugin ( Raven ) {
45
+ function reactNativePlugin ( Raven , options ) {
46
+ options = options || { } ;
47
+
40
48
// react-native doesn't have a document, so can't use default Image
41
49
// transport - use XMLHttpRequest instead
42
50
Raven . setTransport ( reactNativePlugin . _transport ) ;
43
51
44
52
// 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
+ } ) ;
46
56
47
57
var defaultHandler = ErrorUtils . getGlobalHandler && ErrorUtils . getGlobalHandler ( ) || ErrorUtils . _globalHandler ;
48
58
49
59
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 ) ;
53
63
} ) ;
54
64
}
55
65
@@ -92,16 +102,20 @@ reactNativePlugin._transport = function (options) {
92
102
* Strip device-specific IDs found in culprit and frame filenames
93
103
* when running React Native applications on a physical device.
94
104
*/
95
- reactNativePlugin . _normalizeData = function ( data ) {
105
+ reactNativePlugin . _normalizeData = function ( data , pathStripRe ) {
106
+ if ( ! pathStripRe ) {
107
+ pathStripRe = PATH_STRIP_RE ;
108
+ }
109
+
96
110
if ( data . culprit ) {
97
- data . culprit = normalizeUrl ( data . culprit ) ;
111
+ data . culprit = normalizeUrl ( data . culprit , pathStripRe ) ;
98
112
}
99
113
100
114
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
+ } ) ;
105
119
}
106
120
} ;
107
121
0 commit comments