Stack Trace
Show an error's name, message, and frames, with library frames folded away.
Overview
Stack Trace shows an error an agent hit while running your code. The header carries the error's name and message. Below it, the frames from your code are listed in full, and each run of library frames folds into one row, so the frames worth reading come first.
Pass stack, such as error.stack or text from a log, and Stack Trace reads it with parseStackTrace. Or parse it yourself and pass trace.
Anatomy
- HeaderThe error's name, its message, your actions, and Copy
- FrameThe function name and its file, line, and column
- Library rowA folded run of library frames, with its count
Behavior
Frames
Frames keep the order of the trace, newest call first. The first frame from your code is set in medium weight, as the likeliest place to look. Long paths are cut short at the end, and the full location shows on hover.
Library frames
A frame counts as a library frame when its file is in node_modules, is a Node internal, or is native code, such as <anonymous>. Neighboring library frames fold into one row, such as "3 library frames", which opens them in place. Pass defaultShowLibraryFrames to start with them open.
Opening a frame
With onFrameClick, each frame is a button that calls it with the frame and its index, so you can open the file in an editor or a Code View. Without it, frames are plain text.
Copy
Copy puts the original stack on the clipboard, or, for a parsed trace, the trace written in the V8 format. If the browser refuses clipboard access, the block says "Copy failed".
Parsing
parseStackTrace(stack) reads V8 stacks, from Chrome, Edge, and Node, and the function@file:line:column stacks of Firefox and Safari. Lines before the first frame make up the message, so a message can span several lines. It returns { name, message, frames }, and each frame records whether it is a library frame. isLibraryFile(file) is exported for your own checks.
Accessibility
The block is a group named by the error's name. Frames are an ordered list. A library row reports whether it is open. Frame buttons are named for their location, such as "Open lib/export.ts:14:32". The error is marked by its name and an icon as well as color.
Installation
Usage
import { StackTrace, parseStackTrace } from "@/components/ai/stack-trace"
<StackTrace stack={error.stack} />
<StackTrace stack={run.stderr} onFrameClick={(frame) => openFile(frame.file, frame.line)} />
const trace = parseStackTrace(text)
<StackTrace trace={trace} defaultShowLibraryFrames />API reference
StackTrace
Accepts native div props beyond those listed.