158 lines
11 KiB
Plaintext
158 lines
11 KiB
Plaintext
page wrnexustracking {
|
|
seo {
|
|
title = "@wrnexus/tracking"
|
|
description = "Error/event capture, middleware, filtering, and sinks."
|
|
}
|
|
|
|
view {
|
|
<div class="docs-shell">
|
|
<header class="topbar">
|
|
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
|
|
<nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
|
|
<button data-wire-theme-toggle class="theme-button" aria-label="Toggle theme">Theme</button>
|
|
</header>
|
|
<main class="page package-page">
|
|
<aside class="sidebar"><a href="/packages">← All packages</a><span class="category">Runtime</span><h1>@wrnexus/tracking</h1><p>Error/event capture, middleware, filtering, and sinks.</p><code>bun add @wrnexus/tracking@0.2.12</code><nav><a href="#guide">Guide</a><a href="#api">Complete API</a></nav></aside>
|
|
<article class="documentation"><section class="doc-intro"><span class="eyebrow">Runtime</span><h1>@wrnexus/tracking</h1><p>Error/event capture, middleware, filtering, and sinks.</p><pre><code>bun add @wrnexus/tracking@0.2.12</code></pre></section><section id="guide" class="prose"><blockquote>Error tracking for WRNexusJS apps: capture exceptions manually or via middleware and fan them out to pluggable sinks.</blockquote>
|
|
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
|
|
<h3 id="overview">Overview</h3>
|
|
<p><code>@wrnexus/tracking</code> is a small, server-side error-capture layer. You create a tracker with one or more <strong>sinks</strong>, then feed it errors — either manually with <code>tracker.capture(err, context)</code> or automatically by mounting <code>tracker.middleware()</code> in your request pipeline. A <code>consoleSink</code> is included; forwarding to Sentry, Datadog, or any other backend is just a matter of writing a tiny sink. Reach for it when you want a single, sink-agnostic place to route application errors. Sinks run best-effort — a throwing sink never breaks the request.</p>
|
|
<h3 id="installation">Installation</h3>
|
|
<pre data-language="bash"><code>bun add @wrnexus/tracking</code></pre>
|
|
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
|
|
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
|
|
<h3 id="api">API</h3>
|
|
<h4 id="createtracker-options-tracker"><code>createTracker(options?): Tracker</code></h4>
|
|
<p>Creates a tracker. <code>TrackerOptions</code>:</p>
|
|
<div class="table-wrap"><table>
|
|
<thead><tr><th>Option</th><th>Type</th><th>Description</th></tr></thead>
|
|
<tbody><tr><td><code>sinks</code></td><td><code>ErrorSink[]</code></td><td>Initial sinks to fan events out to. Defaults to <code>[]</code>.</td></tr><tr><td><code>now</code></td><td><code>() => number</code></td><td>Clock used for <code>event.timestamp</code> (epoch ms). Defaults to <code>Date.now</code>.</td></tr><tr><td><code>beforeSend</code></td><td>`(event: ErrorEvent) => ErrorEvent \</td><td>null`</td><td>Scrub/enrich an event before it reaches any sink. Return <code>null</code> to drop it.</td></tr></tbody></table></div>
|
|
<p>The returned <code>Tracker</code>:</p>
|
|
<div class="table-wrap"><table>
|
|
<thead><tr><th>Member</th><th>Signature</th><th>Description</th></tr></thead>
|
|
<tbody><tr><td><code>capture</code></td><td><code>(error: unknown, context?: Record<string, unknown>) => Promise<void></code></td><td>Normalizes any thrown value into an <code>Error</code>, builds an <code>ErrorEvent</code>, runs <code>beforeSend</code>, then dispatches to all sinks. Non-<code>Error</code> values are wrapped in an <code>Error</code> named <code>NonError</code>.</td></tr><tr><td><code>addSink</code></td><td><code>(sink: ErrorSink) => void</code></td><td>Registers an additional sink at runtime.</td></tr><tr><td><code>middleware</code></td><td><code>() => Middleware</code></td><td>Returns a WRNexusJS <code>Middleware</code> that captures any error thrown downstream, then re-throws it so the framework's error handler still produces the response.</td></tr></tbody></table></div>
|
|
<p>The middleware attaches this context to captured events:</p>
|
|
<pre data-language="ts"><code>{ method: ctx.req.method, path: ctx.url.pathname, requestId: ctx.locals.requestId }</code></pre>
|
|
<h4 id="consolesink-errorsink"><code>consoleSink: ErrorSink</code></h4>
|
|
<p>A built-in sink that logs a compact one-line message via <code>console.error</code>, e.g. <code>[error] TypeError: cannot read x {"userId":42}</code>.</p>
|
|
<h4 id="types">Types</h4>
|
|
<pre data-language="ts"><code>interface ErrorEvent {
|
|
error: Error;
|
|
context: Record<string, unknown>; // request info, user id, tags…
|
|
timestamp: number; // epoch ms
|
|
}
|
|
|
|
interface ErrorSink {
|
|
name?: string;
|
|
capture(event: ErrorEvent): void | Promise<void>;
|
|
}</code></pre>
|
|
<h3 id="usage">Usage</h3>
|
|
<p>Manual capture:</p>
|
|
<pre data-language="ts"><code>import { createTracker, consoleSink } from "@wrnexus/tracking";
|
|
|
|
const tracker = createTracker({ sinks: [consoleSink] });
|
|
|
|
try {
|
|
await doWork();
|
|
} catch (err) {
|
|
await tracker.capture(err, { userId: 42, op: "doWork" });
|
|
throw err;
|
|
}</code></pre>
|
|
<p>As request middleware:</p>
|
|
<pre data-language="ts"><code>import { createTracker, consoleSink } from "@wrnexus/tracking";
|
|
|
|
const tracker = createTracker({ sinks: [consoleSink] });
|
|
|
|
app.use(tracker.middleware()); // captures + re-throws downstream errors</code></pre>
|
|
<p>A custom sink with <code>beforeSend</code> scrubbing:</p>
|
|
<pre data-language="ts"><code>import { createTracker, type ErrorSink } from "@wrnexus/tracking";
|
|
|
|
const sentrySink: ErrorSink = {
|
|
name: "sentry",
|
|
async capture(event) {
|
|
await Sentry.captureException(event.error, { extra: event.context });
|
|
},
|
|
};
|
|
|
|
const tracker = createTracker({
|
|
sinks: [sentrySink],
|
|
beforeSend(event) {
|
|
delete event.context.password; // scrub secrets
|
|
return event; // return null to drop the event entirely
|
|
},
|
|
});
|
|
|
|
tracker.addSink(anotherSink); // add more sinks later</code></pre>
|
|
<h3 id="requirements-notes">Requirements / Notes</h3>
|
|
<ul>
|
|
<li>Runs on <strong>Bun</strong> only (Node is not supported).</li>
|
|
<li>Peer package: [<code>@wrnexus/core</code>](../core) — the <code>Context</code> and <code>Middleware</code> types</li>
|
|
<p>used by <code>tracker.middleware()</code> come from there.</p>
|
|
<li>Sink dispatch is fire-and-forget-safe: all sinks run via <code>Promise.all</code>, and a</li>
|
|
<p>sink that throws is swallowed so it can never break the app.</p>
|
|
</ul></section><section id="api" class="prose api"><h2>Complete TypeScript API</h2><p>This declaration is generated from the exact published package and lists its exported functions, classes, interfaces, and types.</p><pre data-language="typescript"><code>import { Middleware } from '@wrnexus/core';
|
|
|
|
/**
|
|
* @wrnexus/tracking — error tracking with pluggable sinks. Capture exceptions
|
|
* manually or via middleware, and fan them out to any sink (console by default;
|
|
* write a small sink to forward to Sentry/Datadog/etc.).
|
|
*
|
|
* const tracker = createTracker({ sinks: [consoleSink] });
|
|
* app-middleware: tracker.middleware() // captures + re-throws request errors
|
|
* tracker.capture(err, { userId }); // manual
|
|
*/
|
|
|
|
interface ErrorEvent {
|
|
error: Error;
|
|
/** Arbitrary structured context (request info, user id, tags…). */
|
|
context: Record<string, unknown>;
|
|
/** Epoch ms. */
|
|
timestamp: number;
|
|
}
|
|
interface ErrorSink {
|
|
name?: string;
|
|
capture(event: ErrorEvent): void | Promise<void>;
|
|
}
|
|
interface Tracker {
|
|
capture(error: unknown, context?: Record<string, unknown>): Promise<void>;
|
|
addSink(sink: ErrorSink): void;
|
|
/** Middleware that captures errors thrown downstream, then re-throws them. */
|
|
middleware(): Middleware;
|
|
}
|
|
interface TrackerOptions {
|
|
sinks?: ErrorSink[];
|
|
now?: () => number;
|
|
/** Scrub/enrich an event before it hits sinks (return null to drop it). */
|
|
beforeSend?: (event: ErrorEvent) => ErrorEvent | null;
|
|
}
|
|
/** A sink that logs a compact one-line error to the console. */
|
|
declare const consoleSink: ErrorSink;
|
|
declare function createTracker(options?: TrackerOptions): Tracker;
|
|
|
|
export { type ErrorEvent, type ErrorSink, type Tracker, type TrackerOptions, consoleSink, createTracker };
|
|
</code></pre></section><section id="examples" class="prose examples"><h2>Examples</h2><p>Copy-ready examples taken from this package's published documentation.</p><div class="example-grid"><article class="example-card"><h3>Example 1</h3><pre data-language="bash"><code>bun add @wrnexus/tracking</code></pre></article><article class="example-card"><h3>Example 2</h3><pre data-language="ts"><code>{ method: ctx.req.method, path: ctx.url.pathname, requestId: ctx.locals.requestId }</code></pre></article><article class="example-card"><h3>Example 3</h3><pre data-language="ts"><code>interface ErrorEvent {
|
|
error: Error;
|
|
context: Record<string, unknown>; // request info, user id, tags…
|
|
timestamp: number; // epoch ms
|
|
}
|
|
|
|
interface ErrorSink {
|
|
name?: string;
|
|
capture(event: ErrorEvent): void | Promise<void>;
|
|
}</code></pre></article><article class="example-card"><h3>Example 4</h3><pre data-language="ts"><code>import { createTracker, consoleSink } from "@wrnexus/tracking";
|
|
|
|
const tracker = createTracker({ sinks: [consoleSink] });
|
|
|
|
try {
|
|
await doWork();
|
|
} catch (err) {
|
|
await tracker.capture(err, { userId: 42, op: "doWork" });
|
|
throw err;
|
|
}</code></pre></article></div></section></article>
|
|
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#installation">Installation</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createtracker-options-tracker">createTracker(options?): Tracker</a><a class="toc-level-4" href="#consolesink-errorsink">consoleSink: ErrorSink</a><a class="toc-level-4" href="#types">Types</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
|
|
</main>
|
|
<footer>WRNexusJS 0.2.12 · SSR-first · Bun-native · Documentation generated from published package APIs.</footer>
|
|
</div>
|
|
}
|
|
}
|