From 6aaef900b0c18de82a3a8a5667485ec302fc8515 Mon Sep 17 00:00:00 2001 From: uri Date: Fri, 15 Dec 2023 18:31:41 +0200 Subject: [PATCH] fix stack parse --- src/trace.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/trace.ts b/src/trace.ts index 2007074..feda2ce 100644 --- a/src/trace.ts +++ b/src/trace.ts @@ -6,11 +6,14 @@ 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) }; }; @@ -18,5 +21,7 @@ const parseStackLine = (stackLine: string): StackFrame => { 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"; +};