@sentry/integrations
Version:
Pluggable integrations that can be used to enhance JS SDKs
106 lines (88 loc) • 2.79 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
const utils = require('@sentry/utils');
const WINDOW = utils.GLOBAL_OBJ ;
const DEFAULT_LINES_OF_CONTEXT = 7;
/**
* Collects source context lines around the lines of stackframes pointing to JS embedded in
* the current page's HTML.
*
* This integration DOES NOT work for stack frames pointing to JS files that are loaded by the browser.
* For frames pointing to files, context lines are added during ingestion and symbolication
* by attempting to download the JS files to the Sentry backend.
*
* Use this integration if you have inline JS code in HTML pages that can't be accessed
* by our backend (e.g. due to a login-protected page).
*/
class ContextLines {
/**
* @inheritDoc
*/
static __initStatic() {this.id = 'ContextLines';}
/**
* @inheritDoc
*/
constructor( _options = {}) {this._options = _options;
this.name = ContextLines.id;
}
/**
* @inheritDoc
*/
setupOnce(addGlobalEventProcessor, getCurrentHub) {
addGlobalEventProcessor(event => {
const self = getCurrentHub().getIntegration(ContextLines);
if (!self) {
return event;
}
return this.addSourceContext(event);
});
}
/** Processes an event and adds context lines */
addSourceContext(event) {
const doc = WINDOW.document;
const htmlFilename = WINDOW.location && utils.stripUrlQueryAndFragment(WINDOW.location.href);
if (!doc || !htmlFilename) {
return event;
}
const exceptions = event.exception && event.exception.values;
if (!exceptions || !exceptions.length) {
return event;
}
const html = doc.documentElement.innerHTML;
if (!html) {
return event;
}
const htmlLines = ['<!DOCTYPE html>', '<html>', ...html.split('\n'), '</html>'];
exceptions.forEach(exception => {
const stacktrace = exception.stacktrace;
if (stacktrace && stacktrace.frames) {
stacktrace.frames = stacktrace.frames.map(frame =>
applySourceContextToFrame(
frame,
htmlLines,
htmlFilename,
this._options.frameContextLines != null ? this._options.frameContextLines : DEFAULT_LINES_OF_CONTEXT,
),
);
}
});
return event;
}
} ContextLines.__initStatic();
/**
* Only exported for testing
*/
function applySourceContextToFrame(
frame,
htmlLines,
htmlFilename,
linesOfContext,
) {
if (frame.filename !== htmlFilename || !frame.lineno || !htmlLines.length) {
return frame;
}
utils.addContextToFrame(htmlLines, frame, linesOfContext);
return frame;
}
exports.ContextLines = ContextLines;
exports.applySourceContextToFrame = applySourceContextToFrame;
//# sourceMappingURL=contextlines.js.map