aiellie

Search

Find a component, template or page

GitHub
Components

Stack Trace

An error read from its printed trace: the type and message, opening to its frames, with library frames folded away, file paths that open, and the whole trace to copy.

  1. ProjectList
  2. ProjectsPage

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/stack-trace"

shadcn needs the @aiellie registry in components.json first. npx aiellie add stack-trace adds it on its first run, then installs the same way.

Stack Trace reads an error from the text it was printed as and shows its type and message, opening to its frames. Frames in node_modules and Node's own internals fold away behind one row, so the frames in your code come first. A file path can open the file, and the whole trace can be copied.

It reads V8's frames (Chrome, Node) and Firefox's and Safari's, and leaves out any line it can't read.

Getting started

  1. Pass the trace

    Give it error.stack, or a trace from a log or a tool's output.

    components/run-error.tsx
    import {
      StackTrace,
      StackTraceCopy,
      StackTraceFrames,
      StackTraceHeader,
    } from "@/components/aiellie/stack-trace"
    
    export function RunError({ trace }: { trace: string }) {
      return (
        <StackTrace
          trace={trace}
          onOpenFile={(frame) => openInEditor(frame.file, frame.line)}
        >
          <StackTraceHeader>
            <StackTraceCopy />
          </StackTraceHeader>
          <StackTraceFrames />
        </StackTrace>
      )
    }

Anatomy

<div data-slot="stack-trace"> {/* Base UI Collapsible */}
  <div data-slot="stack-trace-header">
    <button /> {/* the type, the message and the frame count */}
    <button data-slot="stack-trace-copy" />
  </div>
  <div data-slot="stack-trace-frames">
    <ol>
      <li data-library />
    </ol>
  </div>
</div>

The header's children sit at its end, outside the button that opens it. Frames read left to right whatever the page's direction, since they're code. parseStackTrace is exported too, for reading a trace without showing it.

Examples

Show library frames

showLibrary shows every frame from the start.

  1. flattenlib/tree.ts:8:12
  2. Array.map<anonymous>
  3. flattenlib/tree.ts:9:30
  4. Module._compilenode:internal/modules/cjs/loader:1256:14

API reference

StackTrace

PropTypeDefaultDescription
tracestringrequiredThe error's text as it was printed.
onOpenFile(frame: StackFrame) => voidMakes each of your frames' paths a button.

All other Collapsible.Root props are forwarded.

StackTraceFrames

PropTypeDefaultDescription
showLibrarybooleanfalseShows library frames from the start.

StackFrame

FieldTypeDefaultDescription
filestringrequired
namestringThe function, when the trace names one.
line, columnnumber
librarybooleanrequiredIn node_modules or Node's internals.
rawstringrequiredThe line it was read from.

StackTraceCopy takes a tooltip, "Copy trace" by default.