|
1 | | -import type { Event, EventProcessor, Integration, StackFrame } from '@sentry/types'; |
| 1 | +import { convertIntegrationFnToClass } from '@sentry/core'; |
| 2 | +import type { Event, IntegrationFn, StackFrame } from '@sentry/types'; |
2 | 3 | import { LRUMap, addContextToFrame } from '@sentry/utils'; |
3 | 4 |
|
| 5 | +const INTEGRATION_NAME = 'ContextLines'; |
4 | 6 | const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100); |
5 | 7 | const DEFAULT_LINES_OF_CONTEXT = 7; |
6 | 8 |
|
@@ -45,73 +47,54 @@ interface ContextLinesOptions { |
45 | 47 | frameContextLines?: number; |
46 | 48 | } |
47 | 49 |
|
48 | | -/** Add node modules / packages to the event */ |
49 | | -export class ContextLines implements Integration { |
50 | | - /** |
51 | | - * @inheritDoc |
52 | | - */ |
53 | | - public static id = 'ContextLines'; |
54 | | - |
55 | | - /** |
56 | | - * @inheritDoc |
57 | | - */ |
58 | | - public name: string = ContextLines.id; |
59 | | - |
60 | | - public constructor(private readonly _options: ContextLinesOptions = {}) {} |
61 | | - |
62 | | - /** Get's the number of context lines to add */ |
63 | | - private get _contextLines(): number { |
64 | | - return this._options.frameContextLines !== undefined ? this._options.frameContextLines : DEFAULT_LINES_OF_CONTEXT; |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @inheritDoc |
69 | | - */ |
70 | | - public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void): void { |
71 | | - // noop |
72 | | - } |
| 50 | +const denoContextLinesIntegration: IntegrationFn = (options: ContextLinesOptions = {}) => { |
| 51 | + const contextLines = options.frameContextLines !== undefined ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT; |
73 | 52 |
|
74 | | - /** @inheritDoc */ |
75 | | - public processEvent(event: Event): Promise<Event> { |
76 | | - return this.addSourceContext(event); |
77 | | - } |
| 53 | + return { |
| 54 | + name: INTEGRATION_NAME, |
| 55 | + processEvent(event) { |
| 56 | + return addSourceContext(event, contextLines); |
| 57 | + }, |
| 58 | + }; |
| 59 | +}; |
78 | 60 |
|
79 | | - /** Processes an event and adds context lines */ |
80 | | - public async addSourceContext(event: Event): Promise<Event> { |
81 | | - if (this._contextLines > 0 && event.exception && event.exception.values) { |
82 | | - for (const exception of event.exception.values) { |
83 | | - if (exception.stacktrace && exception.stacktrace.frames) { |
84 | | - await this.addSourceContextToFrames(exception.stacktrace.frames); |
85 | | - } |
| 61 | +/** Add node modules / packages to the event */ |
| 62 | +// eslint-disable-next-line deprecation/deprecation |
| 63 | +export const ContextLines = convertIntegrationFnToClass(INTEGRATION_NAME, denoContextLinesIntegration); |
| 64 | + |
| 65 | +/** Processes an event and adds context lines */ |
| 66 | +async function addSourceContext(event: Event, contextLines: number): Promise<Event> { |
| 67 | + if (contextLines > 0 && event.exception && event.exception.values) { |
| 68 | + for (const exception of event.exception.values) { |
| 69 | + if (exception.stacktrace && exception.stacktrace.frames) { |
| 70 | + await addSourceContextToFrames(exception.stacktrace.frames, contextLines); |
86 | 71 | } |
87 | 72 | } |
88 | | - |
89 | | - return event; |
90 | 73 | } |
91 | 74 |
|
92 | | - /** Adds context lines to frames */ |
93 | | - public async addSourceContextToFrames(frames: StackFrame[]): Promise<void> { |
94 | | - const contextLines = this._contextLines; |
95 | | - |
96 | | - for (const frame of frames) { |
97 | | - // Only add context if we have a filename and it hasn't already been added |
98 | | - if (frame.filename && frame.in_app && frame.context_line === undefined) { |
99 | | - const permission = await Deno.permissions.query({ |
100 | | - name: 'read', |
101 | | - path: frame.filename, |
102 | | - }); |
103 | | - |
104 | | - if (permission.state == 'granted') { |
105 | | - const sourceFile = await readSourceFile(frame.filename); |
| 75 | + return event; |
| 76 | +} |
106 | 77 |
|
107 | | - if (sourceFile) { |
108 | | - try { |
109 | | - const lines = sourceFile.split('\n'); |
110 | | - addContextToFrame(lines, frame, contextLines); |
111 | | - } catch (_) { |
112 | | - // anomaly, being defensive in case |
113 | | - // unlikely to ever happen in practice but can definitely happen in theory |
114 | | - } |
| 78 | +/** Adds context lines to frames */ |
| 79 | +async function addSourceContextToFrames(frames: StackFrame[], contextLines: number): Promise<void> { |
| 80 | + for (const frame of frames) { |
| 81 | + // Only add context if we have a filename and it hasn't already been added |
| 82 | + if (frame.filename && frame.in_app && frame.context_line === undefined) { |
| 83 | + const permission = await Deno.permissions.query({ |
| 84 | + name: 'read', |
| 85 | + path: frame.filename, |
| 86 | + }); |
| 87 | + |
| 88 | + if (permission.state == 'granted') { |
| 89 | + const sourceFile = await readSourceFile(frame.filename); |
| 90 | + |
| 91 | + if (sourceFile) { |
| 92 | + try { |
| 93 | + const lines = sourceFile.split('\n'); |
| 94 | + addContextToFrame(lines, frame, contextLines); |
| 95 | + } catch (_) { |
| 96 | + // anomaly, being defensive in case |
| 97 | + // unlikely to ever happen in practice but can definitely happen in theory |
115 | 98 | } |
116 | 99 | } |
117 | 100 | } |
|
0 commit comments