@sentry/browser
Version:
Official Sentry SDK for browsers
267 lines (237 loc) • 7.41 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var utils = require('@sentry/utils');
var helpers = require('../helpers.js');
var DEFAULT_EVENT_TARGET = [
'EventTarget',
'Window',
'Node',
'ApplicationCache',
'AudioTrackList',
'ChannelMergerNode',
'CryptoOperation',
'EventSource',
'FileReader',
'HTMLUnknownElement',
'IDBDatabase',
'IDBRequest',
'IDBTransaction',
'KeyOperation',
'MediaController',
'MessagePort',
'ModalWindow',
'Notification',
'SVGElementInstance',
'Screen',
'TextTrack',
'TextTrackCue',
'TextTrackList',
'WebSocket',
'WebSocketWorker',
'Worker',
'XMLHttpRequest',
'XMLHttpRequestEventTarget',
'XMLHttpRequestUpload',
];
/** Wrap timer functions and event targets to catch errors and provide better meta data */
class TryCatch {
/**
* @inheritDoc
*/
static __initStatic() {this.id = 'TryCatch';}
/**
* @inheritDoc
*/
__init() {this.name = TryCatch.id;}
/** JSDoc */
/**
* @inheritDoc
*/
constructor(options) {;TryCatch.prototype.__init.call(this);
this._options = {
XMLHttpRequest: true,
eventTarget: true,
requestAnimationFrame: true,
setInterval: true,
setTimeout: true,
...options,
};
}
/**
* Wrap timer functions and event targets to catch errors
* and provide better metadata.
*/
setupOnce() {
var global = utils.getGlobalObject();
if (this._options.setTimeout) {
utils.fill(global, 'setTimeout', _wrapTimeFunction);
}
if (this._options.setInterval) {
utils.fill(global, 'setInterval', _wrapTimeFunction);
}
if (this._options.requestAnimationFrame) {
utils.fill(global, 'requestAnimationFrame', _wrapRAF);
}
if (this._options.XMLHttpRequest && 'XMLHttpRequest' in global) {
utils.fill(XMLHttpRequest.prototype, 'send', _wrapXHR);
}
var eventTargetOption = this._options.eventTarget;
if (eventTargetOption) {
var eventTarget = Array.isArray(eventTargetOption) ? eventTargetOption : DEFAULT_EVENT_TARGET;
eventTarget.forEach(_wrapEventTarget);
}
}
} TryCatch.__initStatic();
/** JSDoc */
function _wrapTimeFunction(original) {
return function ( ...args) {
var originalCallback = args[0];
args[0] = helpers.wrap(originalCallback, {
mechanism: {
data: { function: utils.getFunctionName(original) },
handled: true,
type: 'instrument',
},
});
return original.apply(this, args);
};
}
/** JSDoc */
function _wrapRAF(original) {
return function ( callback) {
return original.apply(this, [
helpers.wrap(callback, {
mechanism: {
data: {
function: 'requestAnimationFrame',
handler: utils.getFunctionName(original),
},
handled: true,
type: 'instrument',
},
}),
]);
};
}
/** JSDoc */
function _wrapXHR(originalSend) {
return function ( ...args) {
var xhr = this;
var xmlHttpRequestProps = ['onload', 'onerror', 'onprogress', 'onreadystatechange'];
xmlHttpRequestProps.forEach(prop => {
if (prop in xhr && typeof xhr[prop] === 'function') {
utils.fill(xhr, prop, function (original) {
var wrapOptions = {
mechanism: {
data: {
function: prop,
handler: utils.getFunctionName(original),
},
handled: true,
type: 'instrument',
},
};
// If Instrument integration has been called before TryCatch, get the name of original function
var originalFunction = utils.getOriginalFunction(original);
if (originalFunction) {
wrapOptions.mechanism.data.handler = utils.getFunctionName(originalFunction);
}
// Otherwise wrap directly
return helpers.wrap(original, wrapOptions);
});
}
});
return originalSend.apply(this, args);
};
}
/** JSDoc */
function _wrapEventTarget(target) {
var global = utils.getGlobalObject() ;
var proto = global[target] && global[target].prototype;
if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) {
return;
}
utils.fill(proto, 'addEventListener', function (original)
{
return function (
eventName,
fn,
options,
) {
try {
if (typeof fn.handleEvent === 'function') {
fn.handleEvent = helpers.wrap(fn.handleEvent.bind(fn), {
mechanism: {
data: {
function: 'handleEvent',
handler: utils.getFunctionName(fn),
target,
},
handled: true,
type: 'instrument',
},
});
}
} catch (err) {
// can sometimes get 'Permission denied to access property "handle Event'
}
return original.apply(this, [
eventName,
helpers.wrap(fn , {
mechanism: {
data: {
function: 'addEventListener',
handler: utils.getFunctionName(fn),
target,
},
handled: true,
type: 'instrument',
},
}),
options,
]);
};
});
utils.fill(
proto,
'removeEventListener',
function (
originalRemoveEventListener,
) {
return function (
eventName,
fn,
options,
) {
/**
* There are 2 possible scenarios here:
*
* 1. Someone passes a callback, which was attached prior to Sentry initialization, or by using unmodified
* method, eg. `document.addEventListener.call(el, name, handler). In this case, we treat this function
* as a pass-through, and call original `removeEventListener` with it.
*
* 2. Someone passes a callback, which was attached after Sentry was initialized, which means that it was using
* our wrapped version of `addEventListener`, which internally calls `wrap` helper.
* This helper "wraps" whole callback inside a try/catch statement, and attached appropriate metadata to it,
* in order for us to make a distinction between wrapped/non-wrapped functions possible.
* If a function was wrapped, it has additional property of `__sentry_wrapped__`, holding the handler.
*
* When someone adds a handler prior to initialization, and then do it again, but after,
* then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
* to get rid of the initial handler and it'd stick there forever.
*/
var wrappedEventHandler = fn ;
try {
var originalEventHandler = wrappedEventHandler && wrappedEventHandler.__sentry_wrapped__;
if (originalEventHandler) {
originalRemoveEventListener.call(this, eventName, originalEventHandler, options);
}
} catch (e) {
// ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
}
return originalRemoveEventListener.call(this, eventName, wrappedEventHandler, options);
};
},
);
}
exports.TryCatch = TryCatch;
//# sourceMappingURL=trycatch.js.map