Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/node/src/stack-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function getModule(filename: string | undefined): string | undefined {
}

const FILENAME_MATCH = /^\s*[-]{4,}$/;
const FULL_MATCH = /at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;

// eslint-disable-next-line complexity
const node: StackLineParserFn = (line: string) => {
if (line.match(FILENAME_MATCH)) {
return {
Expand Down Expand Up @@ -87,7 +88,12 @@ const node: StackLineParserFn = (line: string) => {
functionName = undefined;
}

const filename = lineMatch[2];
if (functionName === undefined) {
methodName = methodName || '<anonymous>';
functionName = typeName ? `${typeName}.${methodName}` : methodName;
}

const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].substr(7) : lineMatch[2];
const isNative = lineMatch[5] === 'native';
const isInternal =
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);
Expand All @@ -100,7 +106,7 @@ const node: StackLineParserFn = (line: string) => {
return {
filename,
module: getModule(filename),
function: functionName || `${typeName}.${methodName || '<anonymous>'}`,
function: functionName,
lineno: parseInt(lineMatch[3], 10) || undefined,
colno: parseInt(lineMatch[4], 10) || undefined,
in_app,
Expand Down
88 changes: 86 additions & 2 deletions packages/node/test/stacktrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('Stack parsing', () => {
{
filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js',
module: 'test_case',
function: 'undefined.<anonymous>',
function: '<anonymous>',
lineno: 80,
colno: 10,
in_app: true,
Expand All @@ -238,7 +238,7 @@ describe('Stack parsing', () => {
{
filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js',
module: 'test_case',
function: 'undefined.<anonymous>',
function: '<anonymous>',
lineno: 80,
colno: 10,
in_app: true,
Expand Down Expand Up @@ -294,4 +294,88 @@ describe('Stack parsing', () => {
},
]);
});

test('parses with async frames', () => {
// https://github.com/getsentry/sentry-javascript/issues/4692#issuecomment-1063835795
const err: { [key: string]: any } = {};
err.stack =
'Error: Client request error\n' +
' at Object.httpRequestError (file:///code/node_modules/@waroncancer/gaia/lib/error/error-factory.js:17:73)\n' +
' at Object.run (file:///code/node_modules/@waroncancer/gaia/lib/http-client/http-client.js:81:36)\n' +
' at processTicksAndRejections (node:internal/process/task_queues:96:5)\n' +
' at async Object.send (file:///code/lib/post-created/send-post-created-notification-module.js:17:27)\n' +
' at async each (file:///code/lib/process-post-events-module.js:14:21)\n' +
' at async Runner.processEachMessage (/code/node_modules/kafkajs/src/consumer/runner.js:151:9)\n' +
' at async onBatch (/code/node_modules/kafkajs/src/consumer/runner.js:326:9)\n' +
' at async /code/node_modules/kafkajs/src/consumer/runner.js:376:15\n';

const frames = parseStackFrames(err as Error);

expect(frames).toEqual([
{
filename: '/code/node_modules/kafkajs/src/consumer/runner.js',
module: 'kafkajs.src.consumer:runner',
function: '<anonymous>',
lineno: 376,
colno: 15,
in_app: false,
},
{
filename: '/code/node_modules/kafkajs/src/consumer/runner.js',
module: 'kafkajs.src.consumer:runner',
function: 'onBatch',
lineno: 326,
colno: 9,
in_app: false,
},
{
filename: '/code/node_modules/kafkajs/src/consumer/runner.js',
module: 'kafkajs.src.consumer:runner',
function: 'Runner.processEachMessage',
lineno: 151,
colno: 9,
in_app: false,
},
{
filename: '/code/lib/process-post-events-module.js',
module: 'process-post-events-module',
function: 'each',
lineno: 14,
colno: 21,
in_app: true,
},
{
filename: '/code/lib/post-created/send-post-created-notification-module.js',
module: 'send-post-created-notification-module',
function: 'Object.send',
lineno: 17,
colno: 27,
in_app: true,
},
{
filename: 'node:internal/process/task_queues',
module: 'task_queues',
function: 'processTicksAndRejections',
lineno: 96,
colno: 5,
in_app: false,
},
{
filename: '/code/node_modules/@waroncancer/gaia/lib/http-client/http-client.js',
module: '@waroncancer.gaia.lib.http-client:http-client',
function: 'Object.run',
lineno: 81,
colno: 36,
in_app: false,
},
{
filename: '/code/node_modules/@waroncancer/gaia/lib/error/error-factory.js',
module: '@waroncancer.gaia.lib.error:error-factory',
function: 'Object.httpRequestError',
lineno: 17,
colno: 73,
in_app: false,
},
]);
});
});