@stacktrace-lite/core
Version:
> Parse, filter, and format JavaScript stack traces with plugins for browsers and Node.js.
70 lines (69 loc) • 2.79 kB
TypeScript
import { filterStack, formatStack, StackFrame } from './index.js';
/**
* Options for {@link installBrowserErrorHooks}.
*
* @remarks
* Use these hooks to normalize, filter, and format browser-side error stacks.
* When an error or unhandled promise rejection occurs, the stack is parsed
* into {@link StackFrame} objects, optionally filtered and formatted, and then
* surfaced via {@link BrowserHookOptions.onStack} or logged by default.
*/
export type BrowserHookOptions = {
/**
* Filter configuration passed to {@link filterStack}.
*
* @remarks
* Accepts the same options as {@link filterStack}, such as `include`, `exclude`,
* and `preset` (e.g., `'app-only'` to drop `node_modules` and internals).
*/
filter?: Parameters<typeof filterStack>[1];
/**
* Format configuration passed to {@link formatStack}.
*
* @remarks
* Select an output mode appropriate for your destination (e.g., `'cli'`
* for console/devtools or `'html'` for UI rendering).
*/
format?: Parameters<typeof formatStack>[1];
/**
* Callback invoked with the processed frames and the original event payload.
*
* @param frames - The parsed and (optionally) filtered stack frames.
* @param error - The originating object: an `Error`, an `ErrorEvent` from
* `window.addEventListener('error', ...)`, or a `PromiseRejectionEvent` from
* `window.addEventListener('unhandledrejection', ...)`.
*
* @remarks
* Use this to send telemetry, display a toast, or open an error overlay.
* If omitted, a nicely formatted stack is logged to the console.
*/
onStack?: (frames: StackFrame[], error: Error | Event | PromiseRejectionEvent) => void;
};
/**
* Installs global browser hooks for `error` and `unhandledrejection`.
*
* @param options - Configuration for filtering, formatting, and handling stacks.
*
* @remarks
* - **`error`**: Fired when a script throws or a resource fails; `ErrorEvent.error`
* may contain an `Error` object when available.
* - **`unhandledrejection`**: Fired when a Promise rejects without a handler;
* the event’s `reason` may be an `Error` or any value.
*
* The handler parses the stack via {@link parseStack}, applies {@link filterStack}
* when provided, and then either calls {@link BrowserHookOptions.onStack} or logs
* a formatted stack using {@link formatStack}.
*
* @example
* ```ts
* installBrowserErrorHooks({
* filter: { preset: 'app-only', include: [/\/src\//] },
* format: 'cli', // or 'html'
* onStack(frames, evt) {
* // send to your telemetry backend
* sendToCollector({ frames, kind: evt.type });
* },
* });
* ```
*/
export declare function installBrowserErrorHooks(options?: BrowserHookOptions): void;