Skip to content

Commit 99c24a4

Browse files
authored
Update the serverFunctions.onError reference (#1610)
* Update the `serverFunctions.onError` reference * Document the failure semantics of the awaited `onError` handler * Tighten the wording for a failing `onError` handler
1 parent 8ee50cf commit 99c24a4

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/routes/solid-start/v2/reference/config/solid-start.mdx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ solidStart({
139139

140140
- **Type:** `string`
141141

142-
Path to a module whose default export handles values thrown by server functions before SolidStart serializes them into a response.
142+
Path to a module whose default export handles values thrown by server functions before SolidStart serializes them into a response. Only calls arriving over the network reach it. The module is bundled only into the server, so it can import server-only code.
143143

144-
The handler can report the original value and provide a safer replacement for the client. SolidStart awaits the handler before serializing the response, allowing a monitoring service to flush first.
144+
The handler can report the original value and provide a safer replacement for the client. It may be `async`: SolidStart awaits it before serializing the response, so a monitoring service can flush first. If the handler itself throws or rejects, the original value is sent as if no handler were configured.
145145

146-
Returning or resolving to `undefined` or `null` preserves the original value. A `Response` preserves control flow, while any other value replaces the original. The module is bundled only into the server, so it can import server-only code.
146+
- Return `undefined` or `null` to preserve the original value.
147+
- Return a `Response` unchanged to pass it through, which keeps a thrown `redirect` working. See [Return Responses](/solid-start/v2/advanced/return-responses).
148+
- Return any other value to send it in place of the original, serialized to the client along with its own properties.
147149

148150
```tsx title="vite.config.ts"
149151
solidStart({
@@ -153,17 +155,21 @@ solidStart({
153155
});
154156
```
155157

158+
The module it names, `src/server-function-error.ts`:
159+
156160
```ts title="src/server-function-error.ts"
157161
import type { ServerFunctionErrorHandler } from "@solidjs/start/server";
162+
import { captureException } from "./your-monitoring-client";
158163

159164
const onServerFunctionError: ServerFunctionErrorHandler = (thrown) => {
160-
console.error(thrown);
161-
162-
if (thrown instanceof Error) {
163-
return new Error("The server function failed.");
165+
// redirect() throws a Response, so returning it preserves that control flow.
166+
if (thrown instanceof Response) {
167+
return thrown;
164168
}
165169

166-
return undefined;
170+
captureException(thrown); // or console.error, or any reporter
171+
172+
return new Error("The server function failed.");
167173
};
168174

169175
export default onServerFunctionError;

0 commit comments

Comments
 (0)