@sentry/integrations
Version:
Pluggable integrations that can be used to enhance JS SDKs
85 lines (69 loc) • 1.91 kB
JavaScript
import { CONSOLE_LEVELS, GLOBAL_OBJ, addInstrumentationHandler, severityLevelFromString, addExceptionMechanism, safeJoin } from '@sentry/utils';
/** Send Console API calls as Sentry Events */
class CaptureConsole {
/**
* @inheritDoc
*/
static __initStatic() {this.id = 'CaptureConsole';}
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
constructor(options = {}) {
this.name = CaptureConsole.id;
this._levels = options.levels || CONSOLE_LEVELS;
}
/**
* @inheritDoc
*/
setupOnce(_, getCurrentHub) {
if (!('console' in GLOBAL_OBJ)) {
return;
}
const levels = this._levels;
addInstrumentationHandler('console', ({ args, level }) => {
if (!levels.includes(level)) {
return;
}
const hub = getCurrentHub();
if (!hub.getIntegration(CaptureConsole)) {
return;
}
consoleHandler(hub, args, level);
});
}
} CaptureConsole.__initStatic();
function consoleHandler(hub, args, level) {
hub.withScope(scope => {
scope.setLevel(severityLevelFromString(level));
scope.setExtra('arguments', args);
scope.addEventProcessor(event => {
event.logger = 'console';
addExceptionMechanism(event, {
handled: false,
type: 'console',
});
return event;
});
let message = safeJoin(args, ' ');
const error = args.find(arg => arg instanceof Error);
if (level === 'assert') {
if (args[0] === false) {
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
scope.setExtra('arguments', args.slice(1));
hub.captureMessage(message);
}
} else if (level === 'error' && error) {
hub.captureException(error);
} else {
hub.captureMessage(message);
}
});
}
export { CaptureConsole };
//# sourceMappingURL=captureconsole.js.map