Repository navigation
Expand file tree
/
Copy pathcontextlines.ts
More file actions
104 lines (87 loc) · 3.28 KB
/
Copy pathcontextlines.ts
File metadata and controls
104 lines (87 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { convertIntegrationFnToClass, defineIntegration } from '@sentry/core';
import type { Event, Integration, IntegrationClass, IntegrationFn, StackFrame } from '@sentry/types';
import { GLOBAL_OBJ, addContextToFrame, stripUrlQueryAndFragment } from '@sentry/utils';
const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
const DEFAULT_LINES_OF_CONTEXT = 7;
const INTEGRATION_NAME = 'ContextLines';
interface ContextLinesOptions {
/**
* Sets the number of context lines for each frame when loading a file.
* Defaults to 7.
*
* Set to 0 to disable loading and inclusion of source files.
**/
frameContextLines?: number;
}
const _contextLinesIntegration = ((options: ContextLinesOptions = {}) => {
const contextLines = options.frameContextLines != null ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;
return {
name: INTEGRATION_NAME,
// TODO v8: Remove this
setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
processEvent(event) {
return addSourceContext(event, contextLines);
},
};
}) satisfies IntegrationFn;
export const contextLinesIntegration = defineIntegration(_contextLinesIntegration);
/**
* 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).
*
* @deprecated Use `contextLinesIntegration()` instead.
*/
// eslint-disable-next-line deprecation/deprecation
export const ContextLines = convertIntegrationFnToClass(INTEGRATION_NAME, contextLinesIntegration) as IntegrationClass<
Integration & { processEvent: (event: Event) => Event }
> & { new (options?: { frameContextLines?: number }): Integration };
/**
* Processes an event and adds context lines.
*/
function addSourceContext(event: Event, contextLines: number): Event {
const doc = WINDOW.document;
const htmlFilename = WINDOW.location && 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, contextLines),
);
}
});
return event;
}
/**
* Only exported for testing
*/
export function applySourceContextToFrame(
frame: StackFrame,
htmlLines: string[],
htmlFilename: string,
linesOfContext: number,
): StackFrame {
if (frame.filename !== htmlFilename || !frame.lineno || !htmlLines.length) {
return frame;
}
addContextToFrame(htmlLines, frame, linesOfContext);
return frame;
}