Skip to content

Commit f278e8b

Browse files
committed
Code review changes!
1 parent 7cd79af commit f278e8b

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

packages/node/src/integrations/contextlines.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,32 @@ export class ContextLines implements Integration {
5353
* @inheritDoc
5454
*/
5555
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void {
56-
if (this._options.frameContextLines == undefined) {
56+
// This is only here to copy frameContextLines from init options if it hasn't
57+
// been set via this integrations constructor.
58+
//
59+
// TODO: Remove on next major!
60+
if (this._options.frameContextLines === undefined) {
5761
const initOptions = getCurrentHub().getClient<NodeClient>()?.getOptions();
5862
// eslint-disable-next-line deprecation/deprecation
5963
this._options.frameContextLines = initOptions?.frameContextLines;
6064
}
6165

62-
addGlobalEventProcessor(event => this.addToEvent(event));
63-
}
64-
65-
/** Processes an event and adds context lines */
66-
public async addToEvent(event: Event): Promise<Event> {
6766
const contextLines =
6867
this._options.frameContextLines !== undefined ? this._options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;
6968

69+
addGlobalEventProcessor(event => this.addSourceContext(event, contextLines));
70+
}
71+
72+
/** Processes an event and adds context lines */
73+
public async addSourceContext(event: Event, contextLines: number): Promise<Event> {
7074
const frames = event.exception?.values?.[0].stacktrace?.frames;
7175

7276
if (frames && contextLines > 0) {
73-
const filenames: string[] = [];
77+
const filenames: Set<string> = new Set();
7478

7579
for (const frame of frames) {
76-
if (frame.filename && !filenames.includes(frame.filename)) {
77-
filenames.push(frame.filename);
80+
if (frame.filename) {
81+
filenames.add(frame.filename);
7882
}
7983
}
8084

@@ -91,8 +95,6 @@ export class ContextLines implements Integration {
9195
}
9296
}
9397
}
94-
95-
return event;
9698
}
9799

98100
return event;
@@ -104,25 +106,20 @@ export class ContextLines implements Integration {
104106
*
105107
* @param filenames Array of filepaths to read content from.
106108
*/
107-
async function readSourceFiles(filenames: string[]): Promise<Record<string, string | null>> {
108-
// we're relying on filenames being de-duped already
109-
if (!filenames.length) {
110-
return {};
111-
}
112-
109+
async function readSourceFiles(filenames: Set<string>): Promise<Record<string, string | null>> {
113110
const sourceFiles: Record<string, string | null> = {};
114111

115112
for (const filename of filenames) {
116-
const cache = FILE_CONTENT_CACHE.get(filename);
113+
const cachedFile = FILE_CONTENT_CACHE.get(filename);
117114
// We have a cache hit
118-
if (cache !== undefined) {
115+
if (cachedFile !== undefined) {
119116
// If stored value is null, it means that we already tried, but couldn't read the content of the file. Skip.
120-
if (cache === null) {
117+
if (cachedFile === null) {
121118
continue;
122119
}
123120

124121
// Otherwise content is there, so reuse cached value.
125-
sourceFiles[filename] = cache;
122+
sourceFiles[filename] = cachedFile;
126123
continue;
127124
}
128125

packages/node/src/sdk.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const defaultIntegrations = [
1212
// Common
1313
new CoreIntegrations.InboundFilters(),
1414
new CoreIntegrations.FunctionToString(),
15+
new ContextLines(),
1516
// Native Wrappers
1617
new Console(),
1718
new Http(),
@@ -20,8 +21,6 @@ export const defaultIntegrations = [
2021
new OnUnhandledRejection(),
2122
// Misc
2223
new LinkedErrors(),
23-
// Context Lines
24-
new ContextLines(),
2524
];
2625

2726
/**

packages/node/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface NodeOptions extends Options {
2323
/**
2424
* Sets the number of context lines for each frame when loading a file.
2525
*
26-
* @deprecated Context lines configuration has moved to the `ContextLines` integration.
26+
* @deprecated Context lines configuration has moved to the `ContextLines` integration, and can be used like this:
2727
*
2828
* ```
2929
* init({

packages/node/test/parsers.test.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { getError } from './helper/error';
88

99
describe('parsers.ts', () => {
1010
let frames: stacktrace.StackFrame[];
11-
let spy: jest.SpyInstance;
11+
let readFileSpy: jest.SpyInstance;
1212
let contextLines: ContextLines;
1313

14-
async function addContext(frames: StackFrame[]): Promise<void> {
15-
await contextLines.addToEvent({ exception: { values: [{ stacktrace: { frames } }] } });
14+
async function addContext(frames: StackFrame[], lines: number = 7): Promise<void> {
15+
await contextLines.addSourceContext({ exception: { values: [{ stacktrace: { frames } }] } }, lines);
1616
}
1717

1818
beforeEach(() => {
19-
spy = jest.spyOn(fs, 'readFile');
19+
readFileSpy = jest.spyOn(fs, 'readFile');
2020
frames = stacktrace.parse(new Error('test'));
2121
contextLines = new ContextLines();
2222
resetFileContentCache();
@@ -30,16 +30,16 @@ describe('parsers.ts', () => {
3030
test('parseStack with same file', async () => {
3131
expect.assertions(1);
3232

33-
let mockCalls = 0;
3433
let parsedFrames = Parsers.parseStack(frames);
3534
await addContext(parsedFrames);
3635

37-
mockCalls = spy.mock.calls.length;
36+
const numCalls = readFileSpy.mock.calls.length;
3837
parsedFrames = Parsers.parseStack(frames);
3938
await addContext(parsedFrames);
4039

41-
// Calls to readFile shouldn't increase if there isn't a new error
42-
expect(spy).toHaveBeenCalledTimes(mockCalls);
40+
// Calls to `readFile` shouldn't increase if there isn't a new error to
41+
// parse whose stacktrace contains a file we haven't yet seen
42+
expect(readFileSpy).toHaveBeenCalledTimes(numCalls);
4343
});
4444

4545
test('parseStack with ESM module names', async () => {
@@ -58,27 +58,20 @@ describe('parsers.ts', () => {
5858
];
5959
const parsedFrames = Parsers.parseStack(framesWithFilePath);
6060
await addContext(parsedFrames);
61-
expect(spy).toHaveBeenCalledTimes(1);
61+
expect(readFileSpy).toHaveBeenCalledTimes(1);
6262
});
6363

6464
test('parseStack with adding different file', async () => {
65-
expect.assertions(2);
66-
let mockCalls = 0;
67-
let newErrorCalls = 0;
65+
expect.assertions(1);
6866
let parsedFrames = Parsers.parseStack(frames);
6967
await addContext(parsedFrames);
7068

71-
mockCalls = spy.mock.calls.length;
69+
const numCalls = readFileSpy.mock.calls.length;
7270
parsedFrames = Parsers.parseStack(stacktrace.parse(getError()));
7371
await addContext(parsedFrames);
7472

75-
newErrorCalls = spy.mock.calls.length;
76-
expect(newErrorCalls).toBeGreaterThan(mockCalls);
77-
78-
parsedFrames = Parsers.parseStack(stacktrace.parse(getError()));
79-
await addContext(parsedFrames);
80-
81-
expect(spy).toHaveBeenCalledTimes(newErrorCalls);
73+
const newErrorCalls = readFileSpy.mock.calls.length;
74+
expect(newErrorCalls).toBeGreaterThan(numCalls);
8275
});
8376

8477
test('parseStack with duplicate files', async () => {
@@ -115,16 +108,14 @@ describe('parsers.ts', () => {
115108

116109
const parsedFrames = Parsers.parseStack(framesWithDuplicateFiles);
117110
await addContext(parsedFrames);
118-
expect(spy).toHaveBeenCalledTimes(1);
111+
expect(readFileSpy).toHaveBeenCalledTimes(1);
119112
});
120113

121114
test('parseStack with no context', async () => {
122-
contextLines = new ContextLines({ frameContextLines: 0 });
123-
124115
expect.assertions(1);
125116
const parsedFrames = Parsers.parseStack(frames);
126-
await addContext(parsedFrames);
127-
expect(spy).toHaveBeenCalledTimes(0);
117+
await addContext(parsedFrames, 0);
118+
expect(readFileSpy).toHaveBeenCalledTimes(0);
128119
});
129120
});
130121
});

0 commit comments

Comments
 (0)