Skip to content

Commit 7624320

Browse files
authored
Merge pull request #3518 from solidjs/error-thrower-path
feat(signals,solid,web): error hooks name where an error was thrown apart from where it was met
2 parents 5a0a5f2 + 038470f commit 7624320

16 files changed

Lines changed: 163 additions & 47 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
"@solidjs/web": patch
5+
---
6+
7+
The error hooks and `SSR_RENDER_ERROR_CONTAINED` tell where an error was thrown apart from where it was met.
8+
9+
`ownerPath` on `ClientErrorContext` and `ServerErrorContext` is now where the error was **thrown**: the labels root-first up the owner chain of the computation that threw — the component that broke — falling back to the boundary's chain when the throw crossed nothing the runtime could name. A new `boundaryPath` is where it was **met**: the same labels up the chain of the `<Errored>` that rendered its fallback (client and server) or the `<Loading>` that shipped the rejection (server, `handling: "client"`). Before, `ownerPath` was the boundary's on both sides, so every component under one boundary grouped into one path. On the client the engine's status wrapper already named the thrower (`StatusError.source`); on the server the owner scopes stamp it as the error escapes. The `SSR_RENDER_ERROR_CONTAINED` finding follows: `ownerPath` locates the throw, `data.boundary` / `data.boundaryPath` the boundary.

‎documentation/solid-2.0/03-control-flow.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ A fallback rendered is a failure handled — and, until now, one nothing outside
151151
import { configureClientErrors } from "solid-js";
152152

153153
configureClientErrors({
154-
onError(error, { ownerPath }) {
154+
onError(error, { ownerPath, boundaryPath }) {
155155
Sentry.captureException(error, { mechanism: { type: "solid.error_boundary", handled: true } });
156156
}
157157
});
@@ -162,7 +162,7 @@ render(() => <App />, root, undefined, { onError });
162162

163163
- Fires when an `Errored` (or `createErrorBoundary`) collected a failure and renders its fallback. An **uncaught** error is not this hook's: nothing contained it, the reactive system halts (`REACTIVITY_HALTED`), and the cause goes to the platform's `reportError` — `window.onerror`, the channel every monitor and every `addEventListener("error")` already listens on. One event, one channel.
164164
- **Once per error object**: a `reset()` that recomputes the same failing node re-collects the same error and says nothing new; a primitive thrown has no identity and is reported per sight.
165-
- `ownerPath` carries the component labels root-first where the runtime keeps owner names (the observe and dev artifacts; production owners carry none).
165+
- `ownerPath` is where the error was **thrown** — labels root-first up the owner chain of the computation that threw (component labels and named primitives; the compiler's inner memos ride along by their default name) — and `boundaryPath` where it was **met**, the same labels up the `<Errored>`'s own chain: what broke, and what the user saw. Both where the runtime keeps owner names (the observe and dev artifacts; production owners carry none). When the throw crossed no computation the engine could name, `ownerPath` is the boundary's.
166166
- No return: the client has no wire to map for. A throwing hook is reported on the console and ignored — a monitor never takes the app down.
167167
- Pay-for-use: the hook machinery rides with `createErrorBoundary` or the app's own `configureClientErrors` import; a root's hook is parked on the root owner, so `render` retains nothing for an app that passes none.
168168

‎documentation/solid-2.0/08-dev-diagnostics.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ The server runtime reports on the same channel. Two groups, two tiers. **Finding
460460
- "[SSR_RENDER_ERROR_CONTAINED] Render error in a <Loading> boundary — no boundary could contain it, the request failed: Error: …"
461461
- "[SSR_RENDER_ERROR_CONTAINED] Render error outside any boundary — the request failed: Error: …"
462462

463-
Finding (`error`, observe + dev). A component threw during a server render and the runtime routed the error; `data.handling` says where it went: `"fallback"` — an `<Errored>` rendered its fallback and the response completed normally; `"client"` — the enclosing `<Loading>` fragment rejected and the client re-renders that subtree from scratch after hydration (the response completed, the user paid a client render); `"failed"` — nothing could contain it and the request failed through `onError`. `data.error` is the thrown value as thrown, `data.boundary` the boundary's hydration id, and `ownerPath` locates the boundary. This is the structured face of what `renderToStream`'s per-call `onError` receives: the same errors, on the process-wide channel an observer already subscribes to, with the containment named — so a fallback that renders on every request is visible without anyone watching a 200.
463+
Finding (`error`, observe + dev). A component threw during a server render and the runtime routed the error; `data.handling` says where it went: `"fallback"` — an `<Errored>` rendered its fallback and the response completed normally; `"client"` — the enclosing `<Loading>` fragment rejected and the client re-renders that subtree from scratch after hydration (the response completed, the user paid a client render); `"failed"` — nothing could contain it and the request failed through `onError`. `data.error` is the thrown value as thrown; `ownerPath` locates where it was **thrown** — the labels up the owner chain the error escaped (the component that broke), falling back to the boundary's when the throw crossed no owner — and `data.boundary` / `data.boundaryPath` where it was **met**, the boundary's hydration id and its own labels (what the user saw). This is the structured face of what `renderToStream`'s per-call `onError` receives: the same errors, on the process-wide channel an observer already subscribes to, with the containment named — so a fallback that renders on every request is visible without anyone watching a 200.
464464

465465
#### `SSR_SUBTREE_ABANDONED`
466466

‎documentation/solid-2.0/12-ssr-http.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Everything above is what the runtime does on its own. What it _handles_ — a fa
150150
import { configureServerErrors } from "@solidjs/web";
151151

152152
configureServerErrors({
153-
onError(error, { kind, handling, boundary, functionId, direct, ownerPath, event }) {
153+
onError(error, { kind, handling, boundary, boundaryPath, functionId, direct, ownerPath, event }) {
154154
Sentry.captureException(error, {
155155
mechanism: { type: `solid.${kind}.${handling}`, handled: handling !== "failed" }
156156
});
@@ -163,7 +163,7 @@ renderToStream(code, { onError });
163163
handleServerFunctionRequest(request, { onError });
164164
```
165165

166-
- **Called once per error object, at first sight**, with where the failure was met. `kind: "render"` — `fallback` (an `<Errored>` rendered its fallback; `boundary` is its hydration id, `ownerPath` the component labels when the compiler emitted them), `client` (a `<Loading>` fragment rejected, the client re-renders the subtree), `failed` (nothing contained it; the request fails), `serialize` (a hydration value would not serialize and the render went on without it — reported for a render that passed `onError`, as seroval's own `onError` always was). `kind: "server-function"` — `thrown` (the body threw; `functionId`, and `direct: true` for an in-process call during SSR) or `channel` (a rejection or throw escaping through the result graph, the head already committed). `event` is the request, when the failure happened inside one.
166+
- **Called once per error object, at first sight**, with where the failure was met. `kind: "render"` — `fallback` (an `<Errored>` rendered its fallback; `boundary` is its hydration id and `boundaryPath` its component labels — where the error was **met** — while `ownerPath` is where it was **thrown**, the labels up the owner chain it escaped, when the compiler emitted them), `client` (a `<Loading>` fragment rejected, the client re-renders the subtree), `failed` (nothing contained it; the request fails), `serialize` (a hydration value would not serialize and the render went on without it — reported for a render that passed `onError`, as seroval's own `onError` always was). `kind: "server-function"` — `thrown` (the body threw; `functionId`, and `direct: true` for an in-process call during SSR) or `channel` (a rejection or throw escaping through the result graph, the head already committed). `event` is the request, when the failure happened inside one.
167167
- **The return is the wire value**: rendered into the fallback, serialized for hydration, sent as the RPC error. `undefined` leaves the default policy in place (generic outside dev, fidelity in dev). A returned value is the author's intent — like a `wrapInvocation` mapping — and is not sanitized again; `markSafeError` is not needed on it. Ignored for `failed` and `serialize`, which have no wire. One road a mapping does not reach: a rejected async source's serialized rejection is encoded the moment the source rejects, ahead of the boundary, and carries the default policy's value; the hydrating client renders from the boundary's record, which carries the mapping.
168168
- **Once means once across roads.** A direct server-function call that throws during SSR is met first by the invocation (`kind: "server-function"`, `direct: true`) and then by the `<Errored>` that contains it; the boundary reuses the verdict — the same replacement in the fallback and the record — and does not report again. A `<Loading>` re-pull recurring the same throw is the same object, the same verdict. The observe tier keeps the multi-event picture (an `"invocation"` record _and_ an `SSR_RENDER_ERROR_CONTAINED` finding) for consumers that want it.
169169
- **Two tiers, as `wrapInvocation` has.** Ambient: `configureServerErrors({ onError })`, once per process, registered on `globalThis` under a registered symbol so a bundled server build and an instrumented `--import`ed module share it — and the only tier that sees direct in-process calls. Per request, overriding it: `onError` on `renderToStream`/`renderToString`, and on `handleServerFunctionRequest` for the call it dispatches. A throwing hook is reported on the console and treated as silent. With no hook anywhere, a failure that fails the request goes to `console.error` — never silent.

‎packages/signals/src/boundaries.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,12 @@ export class CollectionQueue extends Queue {
373373
setSignal(this._error!, caught);
374374
// The client error hook: this boundary renders its fallback for
375375
// it — the one road a rendered failure took that no global handler
376-
// ever saw. Once per error object; a `reset()` re-collecting the
377-
// same failure says nothing new.
378-
reportClientError(caught, this._owner);
376+
// ever saw. `source` is the computation that threw (the status
377+
// wrapper's, made at the first landing and kept downstream), so the
378+
// hook hears where it broke as well as where it was met. Once per
379+
// error object; a `reset()` re-collecting the same failure says
380+
// nothing new.
381+
reportClientError(caught, this._owner, source);
379382
}
380383
}
381384
}

‎packages/signals/src/core/error-hooks.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ import type { Owner } from "./types.js";
2727
* boundary carries none of this.
2828
*/
2929
export interface ClientErrorContext {
30-
/** Component labels root-first, when the runtime keeps owner names. */
30+
/**
31+
* Where the error was THROWN: labels root-first up the owner chain of the
32+
* computation that threw — component labels and named primitives — when
33+
* the runtime keeps owner names (the observe and dev artifacts). The
34+
* boundary's own chain when the thrower is unknown (a value thrown
35+
* outside any computation).
36+
*/
3137
ownerPath?: string[];
38+
/**
39+
* Where the error was MET: the same labels up the chain of the `<Errored>`
40+
* that rendered its fallback for it — what the user saw, as against
41+
* `ownerPath`, which is what broke.
42+
*/
43+
boundaryPath?: string[];
3244
}
3345
export type ClientErrorHook = (error: unknown, context: ClientErrorContext) => void;
3446

@@ -81,12 +93,18 @@ function labels(owner: Owner | null | undefined): string[] | undefined {
8193
}
8294

8395
/**
84-
* Tells the client error hook about `error`, caught by a boundary in
85-
* `owner`'s tree — once per error object. A throwing hook is reported on the
86-
* console and otherwise ignored — a monitor must never take the app down.
96+
* Tells the client error hook about `error`, caught by the boundary whose
97+
* owner is `owner`, thrown by `thrower` (the computation the engine's status
98+
* wrapper named; unknown for a value that never crossed one) — once per
99+
* error object. A throwing hook is reported on the console and otherwise
100+
* ignored — a monitor must never take the app down.
87101
* @internal
88102
*/
89-
export function reportClientError(error: unknown, owner: Owner | null | undefined): void {
103+
export function reportClientError(
104+
error: unknown,
105+
owner: Owner | null | undefined,
106+
thrower?: Owner | null
107+
): void {
90108
const isObject = error !== null && (typeof error === "object" || typeof error === "function");
91109
if (isObject) {
92110
if (reported.has(error as object)) return;
@@ -95,8 +113,10 @@ export function reportClientError(error: unknown, owner: Owner | null | undefine
95113
const hook = hookFor(owner);
96114
if (hook === undefined) return;
97115
const context: ClientErrorContext = {};
98-
const path = labels(owner);
116+
const boundary = labels(owner);
117+
const path = labels(thrower) ?? boundary;
99118
if (path !== undefined) context.ownerPath = path;
119+
if (boundary !== undefined) context.boundaryPath = boundary;
100120
try {
101121
hook(error, context);
102122
} catch (hookError) {

‎packages/signals/tests/client-error-hook.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ function boundaryOver(fail: () => boolean, boom: () => unknown) {
4141
let result: unknown;
4242
let reset!: () => void;
4343
createRoot(() => {
44-
const memo = createMemo(() => {
45-
if (fail()) throw boom();
46-
return "content";
47-
});
44+
const memo = createMemo(
45+
() => {
46+
if (fail()) throw boom();
47+
return "content";
48+
},
49+
{ name: "view" }
50+
);
4851
const b = createErrorBoundary(
4952
() => memo(),
5053
(_err, r) => {
@@ -76,7 +79,9 @@ describe("caught", () => {
7679
expect(view.value()).toBe("fallback");
7780
expect(calls).toHaveLength(1);
7881
expect(calls[0].error).toBe(boom);
79-
expect(calls[0].context).toEqual({});
82+
// Where it was thrown: the memo, by name. No labelled owner above it,
83+
// so no boundary path either.
84+
expect(calls[0].context).toEqual({ ownerPath: ["view"] });
8085
});
8186

8287
it("a reset that recomputes the same failure says nothing new; a different error is a new report", () => {
@@ -145,7 +150,10 @@ describe("caught", () => {
145150
flush();
146151
expect(result).toBe("fallback");
147152
expect(calls).toHaveLength(1);
148-
expect(calls[0].context.ownerPath).toEqual(["<App>"]);
153+
// Thrown inside the boundary's own computation under <App>; met by the
154+
// boundary, whose chain is <App>.
155+
expect(calls[0].context.ownerPath).toEqual(["<App>", "computed"]);
156+
expect(calls[0].context.boundaryPath).toEqual(["<App>"]);
149157
});
150158
});
151159

‎packages/solid/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ export interface ServerErrorSite {
151151
kind: "render" | "server-function";
152152
handling: "fallback" | "client" | "failed" | "serialize" | "thrown" | "channel";
153153
boundary?: string;
154+
/** Where the error was thrown — labels root-first up the owner chain it escaped. */
154155
ownerPath?: string[];
156+
/** Where it was met — the labels up the chain of the boundary named by `boundary`. */
157+
boundaryPath?: string[];
155158
functionId?: string;
156159
direct?: boolean;
157160
/** The request event, when the caller has it in hand; else read from the request scope. */

‎packages/solid/src/server/hydration.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
setContext,
1111
runWithBoundaryErrorContext,
1212
RevealGroupContext,
13-
reportServerError
13+
reportServerError,
14+
throwerOf
1415
} from "./signals.js";
1516
import { OBSERVE, ownerPath } from "@solidjs/signals";
1617
import { sharedConfig, NoHydrateContext } from "./shared.js";
@@ -224,9 +225,11 @@ function ssrLoadingBoundary(
224225
? `— the fragment rejected and the client re-renders it: `
225226
: `— no boundary could contain it, the request failed: `) +
226227
errorText(err),
227-
data: { handling, boundary: id, error: err }
228+
data: { handling, boundary: id, boundaryPath: ownerPath(o), error: err }
228229
},
229-
o
230+
// Located where it was THROWN (the owner it escaped), the boundary that
231+
// met it in `data` — the same two facts the server error hook carries.
232+
throwerOf(err) ?? o
230233
);
231234
};
232235
let serializeBuffer: [string, any, boolean?][] = [];

‎packages/solid/src/server/signals.ts‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,32 @@ export function runWithOwner<T>(owner: Owner | null, fn: () => T): T {
295295
currentOwner = owner as unknown as SSROwner | null;
296296
try {
297297
return fn();
298+
} catch (error) {
299+
stampThrower(error, owner);
300+
throw error;
298301
} finally {
299302
currentOwner = prev;
300303
}
301304
}
302305

306+
/**
307+
* Where an error was THROWN, for the server error hook and the findings: the
308+
* innermost owner whose scope it escaped — stamped at the first owner scope
309+
* it crosses (component bodies and effects run through `runWithOwner`, a
310+
* memo's pull through its inlined twin), kept as it propagates to the
311+
* boundary that meets it. Object errors only; a `NotReadyError` is the
312+
* engine's own signal, not a failure.
313+
*/
314+
const throwers = new WeakMap<object, Owner>();
315+
function stampThrower(error: unknown, owner: Owner | null): void {
316+
if (owner === null || !isObject(error) || error instanceof NotReadyError) return;
317+
if (!throwers.has(error)) throwers.set(error, owner);
318+
}
319+
/** @internal The owner `error` was stamped as thrown under, if it crossed one. */
320+
export function throwerOf(error: unknown): Owner | undefined {
321+
return isObject(error) ? throwers.get(error) : undefined;
322+
}
323+
303324
export function getOwner(): Owner | null {
304325
return currentOwner as unknown as Owner | null;
305326
}
@@ -1085,6 +1106,7 @@ function createSyncMemo<T>(
10851106
return value;
10861107
} catch (err) {
10871108
if (err instanceof NotReadyError) throw err; // don't latch — engine re-pulls
1109+
stampThrower(err, owner);
10881110
error = err;
10891111
errored = true;
10901112
cached = true;
@@ -2720,7 +2742,14 @@ export interface ServerErrorSite {
27202742
kind: "render" | "server-function";
27212743
handling: "fallback" | "client" | "failed" | "serialize" | "thrown" | "channel";
27222744
boundary?: string;
2745+
/**
2746+
* Where the error was THROWN: component labels root-first up the owner
2747+
* chain it escaped (the observe and dev builds label owners). The
2748+
* boundary's chain when the thrower is unknown.
2749+
*/
27232750
ownerPath?: string[];
2751+
/** Where it was MET: the labels up the chain of the boundary named by `boundary`. */
2752+
boundaryPath?: string[];
27242753
functionId?: string;
27252754
direct?: boolean;
27262755
/** The request event, when the caller has it in hand; else read from the request scope. */
@@ -2783,10 +2812,13 @@ export function reportServerError(
27832812
const target = hook ?? (ctx && ctx.errorPolicy) ?? ambientServerErrorHook();
27842813
if (target === undefined) return { mapped: false };
27852814
const context: ServerErrorSite = { ...site };
2786-
if (context.ownerPath === undefined && subject) {
2787-
const path = ownerLabels(subject);
2815+
const boundary = subject ? ownerLabels(subject) : undefined;
2816+
if (context.ownerPath === undefined) {
2817+
const thrower = throwerOf(value);
2818+
const path = (thrower !== undefined ? ownerLabels(thrower) : undefined) ?? boundary;
27882819
if (path !== undefined) context.ownerPath = path;
27892820
}
2821+
if (context.boundaryPath === undefined && boundary !== undefined) context.boundaryPath = boundary;
27902822
if (context.event === undefined) {
27912823
const event = currentRequestEvent();
27922824
if (event !== undefined) context.event = event;
@@ -2990,9 +3022,15 @@ export function createErrorBoundary<T, U>(
29903022
kind: "ssr",
29913023
severity: "error",
29923024
message: `[SSR_RENDER_ERROR_CONTAINED] Render error caught by <Errored>: ${text}`,
2993-
data: { handling: "fallback", boundary: owner.id, error: err }
3025+
data: {
3026+
handling: "fallback",
3027+
boundary: owner.id,
3028+
boundaryPath: ownerLabels(owner),
3029+
error: err
3030+
}
29943031
},
2995-
owner
3032+
// Located where it was THROWN, the boundary that met it in `data`.
3033+
throwerOf(err) ?? owner
29963034
);
29973035
};
29983036
// The finding first, with the original; then the one value the client may

0 commit comments

Comments
 (0)