|
10 | 10 | 'use strict';
|
11 | 11 |
|
12 | 12 | var DEVICE_PATH_RE = /^\/var\/mobile\/Containers\/Bundle\/Application\/[^\/]+\/[^\.]+\.app/;
|
| 13 | + |
| 14 | +/** |
| 15 | + * Strip device-specific IDs from React Native file:// paths |
| 16 | + */ |
13 | 17 | function normalizeUrl(url) {
|
14 | 18 | return url
|
15 | 19 | .replace(/^file\:\/\//, '')
|
16 | 20 | .replace(DEVICE_PATH_RE, '');
|
17 | 21 | }
|
18 | 22 |
|
19 |
| -function reactNativePlugin(Raven) { |
20 |
| - function urlencode(obj) { |
21 |
| - var pairs = []; |
22 |
| - for (var key in obj) { |
23 |
| - if ({}.hasOwnProperty.call(obj, key)) |
24 |
| - pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); |
25 |
| - } |
26 |
| - return pairs.join('&'); |
27 |
| - } |
28 |
| - |
29 |
| - function xhrTransport(options) { |
30 |
| - var request = new XMLHttpRequest(); |
31 |
| - request.onreadystatechange = function (e) { |
32 |
| - if (request.readyState !== 4) { |
33 |
| - return; |
34 |
| - } |
35 |
| - |
36 |
| - if (request.status === 200) { |
37 |
| - if (options.onSuccess) { |
38 |
| - options.onSuccess(); |
39 |
| - } |
40 |
| - } else { |
41 |
| - if (options.onError) { |
42 |
| - options.onError(); |
43 |
| - } |
44 |
| - } |
45 |
| - }; |
46 |
| - |
47 |
| - request.open('POST', options.url + '?' + urlencode(options.auth)); |
48 |
| - |
49 |
| - // NOTE: React Native ignores CORS and will NOT send a preflight |
50 |
| - // request for application/json. |
51 |
| - // See: https://facebook.github.io/react-native/docs/network.html#xmlhttprequest |
52 |
| - request.setRequestHeader('Content-type', 'application/json'); |
53 |
| - |
54 |
| - // Sentry expects an Origin header when using HTTP POST w/ public DSN. |
55 |
| - // Just set a phony Origin value; only matters if Sentry Project is configured |
56 |
| - // to whitelist specific origins. |
57 |
| - request.setRequestHeader('Origin', 'react-native://'); |
58 |
| - request.send(JSON.stringify(options.data)); |
| 23 | +/** |
| 24 | + * Extract key/value pairs from an object and encode them for |
| 25 | + * use in a query string |
| 26 | + */ |
| 27 | +function urlencode(obj) { |
| 28 | + var pairs = []; |
| 29 | + for (var key in obj) { |
| 30 | + if ({}.hasOwnProperty.call(obj, key)) |
| 31 | + pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); |
59 | 32 | }
|
| 33 | + return pairs.join('&'); |
| 34 | +} |
60 | 35 |
|
| 36 | +/** |
| 37 | + * Initializes React Native plugin |
| 38 | + */ |
| 39 | +function reactNativePlugin(Raven) { |
61 | 40 | // react-native doesn't have a document, so can't use default Image
|
62 | 41 | // transport - use XMLHttpRequest instead
|
63 |
| - Raven.setTransport(xhrTransport); |
64 |
| - |
| 42 | + Raven.setTransport(reactNativePlugin._transport); |
65 | 43 |
|
66 | 44 | // Use data callback to strip device-specific paths from stack traces
|
67 |
| - Raven.setDataCallback(function (data) { |
68 |
| - if (data.culprit) { |
69 |
| - data.culprit = normalizeUrl(data.culprit); |
70 |
| - } |
71 |
| - |
72 |
| - if (data.exception) { |
73 |
| - // if data.exception exists, all of the other keys are guaranteed to exist |
74 |
| - data.exception.values[0].stacktrace.frames.forEach(function (frame) { |
75 |
| - frame.filename = normalizeUrl(frame.filename); |
76 |
| - }); |
77 |
| - } |
78 |
| - }); |
| 45 | + Raven.setDataCallback(reactNativePlugin._normalizeData); |
79 | 46 |
|
80 | 47 | var defaultHandler = ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler() || ErrorUtils._globalHandler;
|
81 | 48 |
|
82 |
| - ErrorUtils.setGlobalHandler(function(){ |
| 49 | + ErrorUtils.setGlobalHandler(function() { |
83 | 50 | var error = arguments[0];
|
84 | 51 | defaultHandler.apply(this, arguments)
|
85 | 52 | Raven.captureException(error);
|
86 | 53 | });
|
87 | 54 | }
|
88 | 55 |
|
| 56 | +/** |
| 57 | + * Custom HTTP transport for use with React Native applications. |
| 58 | + */ |
| 59 | +reactNativePlugin._transport = function (options) { |
| 60 | + var request = new XMLHttpRequest(); |
| 61 | + request.onreadystatechange = function (e) { |
| 62 | + if (request.readyState !== 4) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (request.status === 200) { |
| 67 | + if (options.onSuccess) { |
| 68 | + options.onSuccess(); |
| 69 | + } |
| 70 | + } else { |
| 71 | + if (options.onError) { |
| 72 | + options.onError(); |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + request.open('POST', options.url + '?' + urlencode(options.auth)); |
| 78 | + |
| 79 | + // NOTE: React Native ignores CORS and will NOT send a preflight |
| 80 | + // request for application/json. |
| 81 | + // See: https://facebook.github.io/react-native/docs/network.html#xmlhttprequest |
| 82 | + request.setRequestHeader('Content-type', 'application/json'); |
| 83 | + |
| 84 | + // Sentry expects an Origin header when using HTTP POST w/ public DSN. |
| 85 | + // Just set a phony Origin value; only matters if Sentry Project is configured |
| 86 | + // to whitelist specific origins. |
| 87 | + request.setRequestHeader('Origin', 'react-native://'); |
| 88 | + request.send(JSON.stringify(options.data)); |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * Strip device-specific IDs found in culprit and frame filenames |
| 93 | + * when running React Native applications on a physical device. |
| 94 | + */ |
| 95 | +reactNativePlugin._normalizeData = function (data) { |
| 96 | + if (data.culprit) { |
| 97 | + data.culprit = normalizeUrl(data.culprit); |
| 98 | + } |
| 99 | + |
| 100 | + 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 | + }); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
89 | 108 | module.exports = reactNativePlugin;
|
0 commit comments