Skip to content

Commit

Permalink
feat: use stacktrace-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 26, 2023
1 parent 2588865 commit 50fa3d9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"name": "Gajus Kuizinas",
"url": "https://gajus.com"
},
"dependencies": {
"stacktrace-parser": "^0.1.10"
},
"description": "V8 stack traces",
"devDependencies": {
"@semantic-release/commit-analyzer": "^11.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/getStackTrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import { expect, test } from 'vitest';
test('gets stack trace', () => {
const stackTrace = getStackTrace();

expect(stackTrace.callSites[0].fileName).toMatch(/getStackTrace/u);
expect(stackTrace[0].fileName).toMatch(/getStackTrace/u);
expect(stackTrace[0].lineNumber).toBe(5);
expect(stackTrace[0].columnNumber).toBe(22);
});
51 changes: 23 additions & 28 deletions src/getStackTrace.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import { type StackTrace } from './types';
import { parse as parseStackTrace } from 'stacktrace-parser';

export const getStackTrace = (): StackTrace => {
const oldStackTraceLimit = Error.stackTraceLimit;
const oldPrepareStackTrace = Error.prepareStackTrace;

Error.prepareStackTrace = (error, structuredStackTrace) => {
return structuredStackTrace;
};

const honeypot: { stack: NodeJS.CallSite[] } = {
stack: [],
};

Error.captureStackTrace(honeypot);

const callSites = honeypot.stack;

Error.stackTraceLimit = oldStackTraceLimit;
Error.prepareStackTrace = oldPrepareStackTrace;

const trail: readonly NodeJS.CallSite[] = callSites.slice(1);

return {
callSites: trail.map((callSite) => {
// The reason we are parsing the stack trace rather than
// using captureStackTrace is because captureStackTrace
// does not resolve source maps, i.e. the stack trace
// will contain the compiled code references rather than
// the original source code references.
//
// eslint-disable-next-line unicorn/error-message
const stackTrace = new Error().stack;

if (!stackTrace) {
throw new Error('Could not get stack trace');
}

return parseStackTrace(stackTrace)
.map((stackFrame) => {
return {
columnNumber: callSite.getColumnNumber(),
fileName: callSite.getFileName() ?? null,
functionName: callSite.getFunctionName(),
lineNumber: callSite.getLineNumber(),
arguments: stackFrame.arguments,
columnNumber: stackFrame.column,
fileName: stackFrame.file,
functionName: stackFrame.methodName,
lineNumber: stackFrame.lineNumber,
};
}),
};
})
.slice(1);
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { getStackTrace } from './getStackTrace';
export { serializeStackTrace } from './serializeStackTrace';
export { CallSite, StackTrace } from './types';
export { StackFrame, StackTrace } from './types';
2 changes: 1 addition & 1 deletion src/serializeStackTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const serializeStackTrace = (
': ' +
errorMessage +
'\n' +
stackTrace.callSites
stackTrace
.map((stackFrame) => {
const { columnNumber, fileName, functionName, lineNumber } = stackFrame;

Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
export type CallSite = {
export type StackFrame = {
arguments: readonly string[];
columnNumber: number | null;
fileName: string | null;
functionName: string | null;
lineNumber: number | null;
};

export type StackTrace = {
callSites: CallSite[];
};
export type StackTrace = StackFrame[];

0 comments on commit 50fa3d9

Please sign in to comment.