Skip to content

Commit

Permalink
fix stack parse
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Dec 15, 2023
1 parent 6e7e77a commit 6aaef90
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ interface StackFrame {
const frameToString = ({ line, file, column }: StackFrame) =>
`${file}:${line}:${column}`;

const parseStackLine = (stackLine: string): StackFrame => {
const parseStackLine = (stackLine: string): StackFrame | null => {
const matches = RegExp(/\s+at\s+(.+\s)?\(?(.+):(\d+):(\d+)\)?/).exec(
stackLine,
);
if (!matches) throw new Error("could not figure out stack line");
if (!matches) {
console.error(`could not figure out stack line ${stackLine}`);
return null;
}
const [, , file, line, column] = matches;
return { file, line: parseInt(line), column: parseInt(column) };
};

const parseStackTrace = (trace: string, picker: (lines: string[]) => string) =>
parseStackLine(picker(trace.split("\n")));

export const currentLocation = (depth: number) =>
frameToString(parseStackTrace(new Error().stack as string, (x) => x[depth]));
export const currentLocation = (depth: number) => {
const x = parseStackTrace(new Error().stack as string, (x) => x[depth]);
return x ? frameToString(x) : "stack line could not be parsed";
};

0 comments on commit 6aaef90

Please sign in to comment.