Parse V8 Stack Traces Into Frames

Turn V8 stack strings into structured frames with function names, file paths, line and column numbers, async and native markers.

TypescriptCli Tooling

Code

typescriptsnippet
78 lines
export interface StackFrame {
  readonly functionName: string | null;
  readonly file: string | null;
  readonly line: number | null;
  readonly column: number | null;
  readonly native: boolean;
  readonly async: boolean;
}

export function parseStackTrace(stack: string): StackFrame[] {
  if (typeof stack !== "string") throw new TypeError("stack must be a string");
  const frames: StackFrame[] = [];
  for (const rawLine of stack.split("\n")) {
    const line = rawLine.trim();
    if (!line.startsWith("at ")) continue;
    const frame = parseFrame(line.slice(3));
    if (frame !== null) frames.push(frame);
  }
  return frames;
}

function parseFrame(value: string): StackFrame | null {
  if (value === "native") {
    return { functionName: null, file: null, line: null, column: null, native: true, async: false };
  }
  let text = value;
  let isAsync = false;
  if (text.startsWith("async ")) {
    isAsync = true;
    text = text.slice(6);
  }
  const locationMatch = /^(.*?) \((.+):(\d+):(\d+)\)$/.exec(text);
  if (locationMatch !== null) {
    return {
      functionName: normalizeName(locationMatch[1] ?? ""),
      file: locationMatch[2] ?? null,
      line: Number(locationMatch[3]),
      column: Number(locationMatch[4]),
      native: false,
      async: isAsync,
    };
  }
  const bareMatch = /^(.+):(\d+):(\d+)$/.exec(text);
  if (bareMatch !== null) {
    return {
      functionName: null,
      file: bareMatch[1] ?? null,
      line: Number(bareMatch[2]),
      column: Number(bareMatch[3]),
      native: false,
      async: isAsync,
    };
  }
  return {
    functionName: normalizeName(text),
    file: null,
    line: null,
    column: null,
    native: false,
    async: isAsync,
  };
}

function normalizeName(value: string): string | null {
  const name = value.trim();
  if (name === "" || name === "eval") return null;
  return name;
}

const stack = [
  "Error: boom",
  "    at fetchUser (/app/src/users.js:42:13)",
  "    at async loadProfile (/app/src/profile.js:8:20)",
  "    at /app/src/index.js:3:1",
  "    at native",
].join("\n");
console.log(parseStackTrace(stack));
console.log(parseStackTrace("Error: empty"));

Inside the snippet

Structurally, expect the functions parseStackTrace, parseFrame and normalizeName and nothing else.

One loop does the cycling here: for (const rawLine of stack.split("\n")). Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. Invalid states are rejected up front: it throws TypeError instead of continuing with bad data.

The result comes back as an object carrying null, true.

What to watch for

  • The guard typeof stack !== "string" short-circuits the flow with an early throw when its condition holds.

Using it

The main signature reads export function parseStackTrace(stack: string)

parseStackTrace is what another module would consume; nothing else here is load-bearing for callers.

The call returns null, true — destructure what you need at the call site.

Key statements

return { functionName: null, file: null, line: null, column: null, native: true, async: false };
if (typeof stack !== "string") throw new TypeError("stack must be a string");
if (name === "" || name === "eval") return null;

Interactive Tools for This Topic

FAQ

Does `parseStackTrace` modify its input?
No — a new value is constructed for the result and the arguments are never written to.
Does the code validate its input?
It throws `TypeError` before any real work runs, via the guard `typeof stack !== "string"` — handle it at the call site or let it propagate deliberately.
Does the runtime need any help from npm, pip, cargo, or the like?
No — the package manager has nothing to do here; nothing is declared. The file defines `parseStackTrace`, `parseFrame`, `normalizeName` at load time.