Skip to content

Commit 9cac1a0

Browse files
committed
Add react-native plugin
1 parent 38cb3e9 commit 9cac1a0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

plugins/react-native.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*global ErrorUtils:false*/
2+
3+
/**
4+
* react-native plugin for Raven
5+
*
6+
* Usage:
7+
* var Raven = require('raven-js');
8+
* require('raven-js/plugins/react-native')(Raven);
9+
*/
10+
module.exports = function (Raven) {
11+
"use strict";
12+
13+
function urlencode(obj) {
14+
var pairs = [];
15+
for (var key in obj) {
16+
if ({}.hasOwnProperty.call(obj, key))
17+
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
18+
}
19+
return pairs.join('&');
20+
}
21+
22+
function xhrTransport(options) {
23+
options.auth.sentry_data = JSON.stringify(options.data);
24+
25+
var request = new XMLHttpRequest();
26+
request.onreadystatechange = function (e) {
27+
if (request.readyState !== 4) {
28+
return;
29+
}
30+
31+
if (request.status === 200) {
32+
if (options.onSuccess) {
33+
options.onSuccess;
34+
}
35+
} else {
36+
if (options.onError) {
37+
options.onError();
38+
}
39+
}
40+
};
41+
42+
request.open('GET', options.url + '?' + urlencode(options.auth));
43+
request.send();
44+
}
45+
46+
// react-native doesn't have a document, so can't use default Image
47+
// transport - use XMLHttpRequest instead
48+
Raven.setTransport(xhrTransport);
49+
50+
ErrorUtils.setGlobalHandler(Raven.captureException);
51+
};

0 commit comments

Comments
 (0)