rollbar
Version:
Effortlessly track and debug errors in your JavaScript applications with Rollbar. This package includes advanced error tracking features and an intuitive interface to help you identify and fix issues more quickly.
171 lines (154 loc) • 3.85 kB
JavaScript
import * as _ from '../utility.js';
import makeFetchRequest from './transport/fetch.js';
import makeXhrRequest from './transport/xhr.js';
/*
* accessToken may be embedded in payload but that should not
* be assumed
*
* options: {
* hostname
* protocol
* path
* port
* method
* transport ('xhr' | 'fetch')
* }
*
* params is an object containing key/value pairs. These
* will be appended to the path as 'key=value&key=value'
*
* payload is an unserialized object
*/
function Transport(truncation) {
this.truncation = truncation;
}
Transport.prototype.get = function (
accessToken,
options,
params,
callback,
requestFactory,
) {
if (!callback || !_.isFunction(callback)) {
callback = () => {};
}
_.addParamsAndAccessTokenToPath(accessToken, options, params);
var method = 'GET';
var url = _.formatUrl(options);
this._makeZoneRequest({
accessToken,
url,
method,
callback,
requestFactory,
timeout: options.timeout,
transport: options.transport,
});
};
Transport.prototype.post = function ({
accessToken,
options,
payload,
headers,
callback,
requestFactory,
}) {
if (!callback || !_.isFunction(callback)) {
callback = () => {};
}
if (!payload) {
return callback(new Error('Cannot send empty request'));
}
var stringifyResult;
// Check payload.body to ensure only items are truncated.
if (this.truncation && payload.body) {
stringifyResult = this.truncation.truncate(payload);
} else {
stringifyResult = _.stringify(payload);
}
if (stringifyResult.error) {
return callback(stringifyResult.error);
}
var method = 'POST';
var url = _.formatUrl(options);
this._makeZoneRequest({
accessToken,
url,
method,
payload: stringifyResult.value,
headers,
callback,
requestFactory,
timeout: options.timeout,
transport: options.transport,
});
};
Transport.prototype.postJsonPayload = function (
accessToken,
options,
payload,
callback,
requestFactory,
) {
if (!callback || !_.isFunction(callback)) {
callback = () => {};
}
var method = 'POST';
var url = _.formatUrl(options);
this._makeZoneRequest({
accessToken,
url,
method,
payload,
callback,
requestFactory,
timeout: options.timeout,
transport: options.transport,
});
};
// Wraps `_makeRequest` if zone.js is being used, ensuring that Rollbar
// API calls are not intercepted by any child forked zones.
// This is equivalent to `NgZone.runOutsideAngular` in Angular.
Transport.prototype._makeZoneRequest = function () {
var gWindow =
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self);
// Whenever zone.js is loaded and `Zone` is exposed globally, access
// the root zone to ensure that requests are always made within it.
// This approach is framework-agnostic, regardless of which
// framework zone.js is used with.
var rootZone = gWindow && gWindow.Zone && gWindow.Zone.root;
var args = Array.prototype.slice.call(arguments);
if (rootZone) {
rootZone.run(() => {
this._makeRequest.apply(undefined, args);
});
} else {
this._makeRequest.apply(undefined, args);
}
};
Transport.prototype._makeRequest = function (params) {
const { payload, callback, transport } = params;
if (typeof RollbarProxy !== 'undefined') {
return _proxyRequest(payload, callback);
}
if (transport === 'fetch') {
makeFetchRequest(params);
} else {
makeXhrRequest(params);
}
};
/* global RollbarProxy */
function _proxyRequest(json, callback) {
var rollbarProxy = new RollbarProxy();
rollbarProxy.sendJsonPayload(
json,
function (_msg) {
/* do nothing */
},
function (err) {
callback(new Error(err));
},
);
}
export default Transport;