diff --git a/plugins/react-native.js b/plugins/react-native.js new file mode 100644 index 000000000000..27262a01f9b9 --- /dev/null +++ b/plugins/react-native.js @@ -0,0 +1,51 @@ +/*global ErrorUtils:false*/ + +/** + * react-native plugin for Raven + * + * Usage: + * var Raven = require('raven-js'); + * require('raven-js/plugins/react-native')(Raven); + */ +module.exports = function (Raven) { + "use strict"; + + function urlencode(obj) { + var pairs = []; + for (var key in obj) { + if ({}.hasOwnProperty.call(obj, key)) + pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); + } + return pairs.join('&'); + } + + function xhrTransport(options) { + options.auth.sentry_data = JSON.stringify(options.data); + + var request = new XMLHttpRequest(); + request.onreadystatechange = function (e) { + if (request.readyState !== 4) { + return; + } + + if (request.status === 200) { + if (options.onSuccess) { + options.onSuccess(); + } + } else { + if (options.onError) { + options.onError(); + } + } + }; + + request.open('GET', options.url + '?' + urlencode(options.auth)); + request.send(); + } + + // react-native doesn't have a document, so can't use default Image + // transport - use XMLHttpRequest instead + Raven.setTransport(xhrTransport); + + ErrorUtils.setGlobalHandler(Raven.captureException); +}; diff --git a/src/raven.js b/src/raven.js index 1f2638154645..1ee94c5a6e52 100644 --- a/src/raven.js +++ b/src/raven.js @@ -359,6 +359,21 @@ var Raven = { return Raven; }, + /** + * Override the default HTTP transport mechanism that transmits data + * to the Sentry server. + * + * @param {function} transport Function invoked instead of the default + * `makeRequest` handler. + * + * @return {Raven} + */ + setTransport: function(transport) { + globalOptions.transport = transport; + + return Raven; + }, + /* * Get the latest raw exception that was captured by Raven. *