Skip to content

Commit 7ff638e

Browse files
committed
Clean up the serverFunctions.onError JSDoc
1 parent d34cb89 commit 7ff638e

3 files changed

Lines changed: 68 additions & 14 deletions

File tree

apps/tests/src/server-fn-error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ServerFunctionErrorHandler } from "@solidjs/start/server";
22

33
/**
4-
* Wired up through `serverFunctions.onError`. Unlike `seroval-plugins.ts` this
5-
* module is bundled into the server only, so it may reach for server-only code.
4+
* Wired up through `serverFunctions.onError`. Unlike `src/seroval-plugins.ts`,
5+
* this module is bundled into the server only, so it may reach for server-only
6+
* code.
67
*
78
* Returning `undefined` sends whatever was thrown, which is what leaves the
89
* other server-function error tests in this app seeing their own errors.

packages/start/src/config/index.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,45 @@ export interface SolidStartOptions {
141141
env?: EnvPluginOptions;
142142

143143
/**
144-
* Options controlling which files are processed as server functions
145-
* (inclusion / exclusion filters for the `"use server"` transform).
144+
* Options for server functions: which files the `"use server"` transform
145+
* processes (inclusion / exclusion filters), and what happens when a server
146+
* function throws.
146147
*/
147148
serverFunctions?: Pick<ServerFunctionsOptions, "filter"> & {
148149
/**
149-
* Path to a module whose default export is called with whatever a server
150-
* function threw, before it is serialized into the response. Return a
151-
* value to send it in place of what was thrown, or `undefined` to send the
152-
* original.
150+
* Path to a module whose default export handles whatever a server function
151+
* throws, before it reaches the client. Use it to report failures to a
152+
* monitoring service, or to replace an error carrying internal detail with
153+
* one that is safe to send.
153154
*
154-
* Naming the module here rather than registering a handler at runtime
155-
* keeps the app in sole control of it: no dependency can reach into the
156-
* running server and take over reporting.
155+
* Only server function calls made over the network run through it. A
156+
* server function called during rendering runs in process and throws
157+
* straight to its caller, and errors from API routes never reach it
158+
* either.
157159
*
158-
* The module is bundled into the server only, so it may import server-only
159-
* code such as a monitoring SDK.
160+
* The export is called synchronously with the thrown value, and what it
161+
* returns decides what the client sees:
162+
*
163+
* - `undefined` (or `null`) sends what was thrown, unchanged.
164+
* - A `Response` is passed through unchanged, which keeps a thrown
165+
* `redirect()` working.
166+
* - Any other value is sent in place of what was thrown, and the client
167+
* call rejects with it.
168+
*
169+
* Control flow reaches the export the same way errors do, so a handler
170+
* that replaces everything it sees turns redirects into errors.
171+
*
172+
* Whatever is sent gets serialized to the client along with its own
173+
* properties, so an error meant to be safe to expose must not carry
174+
* internal detail.
175+
*
176+
* The return value is not awaited, so make the export a plain function
177+
* rather than an `async` one, and report failures with calls that do not
178+
* need awaiting.
179+
*
180+
* Type the export as `ServerFunctionErrorHandler` from
181+
* `@solidjs/start/server`. The module is bundled into the server only, so
182+
* it may import server-only code such as a monitoring SDK.
160183
*
161184
* @example "src/server-fn-error.ts"
162185
*/
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
import onServerFunctionError from "solid-start:server-fn-error-handler";
22

3+
/**
4+
* Handles whatever a server function threw, before it reaches the client. Use
5+
* it to type the default export of the module named by the
6+
* `serverFunctions.onError` option in `vite.config.ts`.
7+
*
8+
* Called synchronously, and the value it returns is not awaited, so it cannot
9+
* be an `async` function.
10+
*
11+
* @param thrown The value the server function threw. This is a `Response` when
12+
* the server function threw control flow such as a `redirect()`.
13+
* @returns `undefined` (or `null`) to send `thrown` unchanged, a `Response` to
14+
* pass control flow through untouched, or any other value to send in place of
15+
* `thrown`.
16+
*
17+
* @example
18+
* ```ts
19+
* // src/server-fn-error.ts
20+
* import type { ServerFunctionErrorHandler } from "@solidjs/start/server";
21+
* import { captureException } from "./your-monitoring-client";
22+
*
23+
* const onServerFunctionError: ServerFunctionErrorHandler = thrown => {
24+
* // redirect() throws a Response, so returning it preserves that control flow.
25+
* if (thrown instanceof Response) return thrown;
26+
*
27+
* captureException(thrown); // or console.error, or any reporter
28+
* return new Error("Something went wrong");
29+
* };
30+
*
31+
* export default onServerFunctionError;
32+
* ```
33+
*/
334
export type ServerFunctionErrorHandler = (thrown: unknown) => unknown;
435

5-
/** @internal */
636
export function applyServerFunctionErrorHandler(thrown: unknown): unknown {
737
return onServerFunctionError?.(thrown) ?? thrown;
838
}

0 commit comments

Comments
 (0)