-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Decode
filename
and module
stack frame properties in N…
…ode stack parser (#14544) In ESM, the Node stack trace filenames are URL-encoded. This patch: - decodes the `filename` stack frame property - decodes the `module` property (which is derived from the raw filename) - adds a bunch of unit tests for the module name extraction logic (general tests as well as for encoded file names) - adds unit and integration tests (ESM and CJS) for file names with spaces
- Loading branch information
Showing
8 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/contextLines/instrument.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
autoSessionTracking: false, | ||
transport: loggingTransport, | ||
}); |
13 changes: 13 additions & 0 deletions
13
dev-packages/node-integration-tests/suites/contextLines/scenario with space.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Sentry = require('@sentry/node'); | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
autoSessionTracking: false, | ||
transport: loggingTransport, | ||
}); | ||
|
||
Sentry.captureException(new Error('Test Error')); | ||
|
||
// some more post context |
5 changes: 5 additions & 0 deletions
5
dev-packages/node-integration-tests/suites/contextLines/scenario with space.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.captureException(new Error('Test Error')); | ||
|
||
// some more post context |
83 changes: 83 additions & 0 deletions
83
dev-packages/node-integration-tests/suites/contextLines/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { join } from 'path'; | ||
import { conditionalTest } from '../../utils'; | ||
import { createRunner } from '../../utils/runner'; | ||
|
||
conditionalTest({ min: 18 })('ContextLines integration in ESM', () => { | ||
test('reads encoded context lines from filenames with spaces', done => { | ||
expect.assertions(1); | ||
const instrumentPath = join(__dirname, 'instrument.mjs'); | ||
|
||
createRunner(__dirname, 'scenario with space.mjs') | ||
.withFlags('--import', instrumentPath) | ||
.expect({ | ||
event: { | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'Test Error', | ||
stacktrace: { | ||
frames: expect.arrayContaining([ | ||
{ | ||
filename: expect.stringMatching(/\/scenario with space.mjs$/), | ||
context_line: "Sentry.captureException(new Error('Test Error'));", | ||
pre_context: ["import * as Sentry from '@sentry/node';", ''], | ||
post_context: ['', '// some more post context'], | ||
colno: 25, | ||
lineno: 3, | ||
function: '?', | ||
in_app: true, | ||
module: 'scenario with space', | ||
}, | ||
]), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); | ||
|
||
describe('ContextLines integration in CJS', () => { | ||
test('reads context lines from filenames with spaces', done => { | ||
expect.assertions(1); | ||
|
||
createRunner(__dirname, 'scenario with space.cjs') | ||
.expect({ | ||
event: { | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'Test Error', | ||
stacktrace: { | ||
frames: expect.arrayContaining([ | ||
{ | ||
filename: expect.stringMatching(/\/scenario with space.cjs$/), | ||
context_line: "Sentry.captureException(new Error('Test Error'));", | ||
pre_context: [ | ||
'Sentry.init({', | ||
" dsn: 'https://[email protected]/1337',", | ||
" release: '1.0',", | ||
' autoSessionTracking: false,', | ||
' transport: loggingTransport,', | ||
'});', | ||
'', | ||
], | ||
post_context: ['', '// some more post context'], | ||
colno: 25, | ||
lineno: 11, | ||
function: 'Object.?', | ||
in_app: true, | ||
module: 'scenario with space', | ||
}, | ||
]), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createGetModuleFromFilename } from '../../src'; | ||
|
||
describe('createGetModuleFromFilename', () => { | ||
it.each([ | ||
['/path/to/file.js', 'file'], | ||
['/path/to/file.mjs', 'file'], | ||
['/path/to/file.cjs', 'file'], | ||
['file.js', 'file'], | ||
])('returns the module name from a filename %s', (filename, expected) => { | ||
const getModule = createGetModuleFromFilename(); | ||
expect(getModule(filename)).toBe(expected); | ||
}); | ||
|
||
it('applies the given base path', () => { | ||
const getModule = createGetModuleFromFilename('/path/to/base'); | ||
expect(getModule('/path/to/base/file.js')).toBe('file'); | ||
}); | ||
|
||
it('decodes URI-encoded file names', () => { | ||
const getModule = createGetModuleFromFilename(); | ||
expect(getModule('/path%20with%space/file%20with%20spaces(1).js')).toBe('file with spaces(1)'); | ||
}); | ||
|
||
it('returns undefined if no filename is provided', () => { | ||
const getModule = createGetModuleFromFilename(); | ||
expect(getModule(undefined)).toBeUndefined(); | ||
}); | ||
|
||
it.each([ | ||
['/path/to/base/node_modules/@sentry/test/file.js', '@sentry.test:file'], | ||
['/path/to/base/node_modules/somePkg/file.js', 'somePkg:file'], | ||
])('handles node_modules file paths %s', (filename, expected) => { | ||
const getModule = createGetModuleFromFilename(); | ||
expect(getModule(filename)).toBe(expected); | ||
}); | ||
|
||
it('handles windows paths with passed basePath and node_modules', () => { | ||
const getModule = createGetModuleFromFilename('C:\\path\\to\\base', true); | ||
expect(getModule('C:\\path\\to\\base\\node_modules\\somePkg\\file.js')).toBe('somePkg:file'); | ||
}); | ||
|
||
it('handles windows paths with default basePath', () => { | ||
const getModule = createGetModuleFromFilename(undefined, true); | ||
expect(getModule('C:\\path\\to\\base\\somePkg\\file.js')).toBe('file'); | ||
}); | ||
}); |