UNPKG

@sentry/integrations

Version:
87 lines (70 loc) 1.93 kB
Object.defineProperty(exports, '__esModule', { value: true }); const utils = require('@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 || utils.CONSOLE_LEVELS; } /** * @inheritDoc */ setupOnce(_, getCurrentHub) { if (!('console' in utils.GLOBAL_OBJ)) { return; } const levels = this._levels; utils.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(utils.severityLevelFromString(level)); scope.setExtra('arguments', args); scope.addEventProcessor(event => { event.logger = 'console'; utils.addExceptionMechanism(event, { handled: false, type: 'console', }); return event; }); let message = utils.safeJoin(args, ' '); const error = args.find(arg => arg instanceof Error); if (level === 'assert') { if (args[0] === false) { message = `Assertion failed: ${utils.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); } }); } exports.CaptureConsole = CaptureConsole; //# sourceMappingURL=captureconsole.js.map