Skip to content

Commit a8a1d13

Browse files
authored
fix: error boundary (#7342)
* feat: add ErrorBoundary component and update error handling in Qwik City * fix: update error boundary handling in Qwik City and Qwik * fix: improve ErrorBoundary handling and update useErrorBoundary return value
1 parent 07a45f4 commit a8a1d13

File tree

12 files changed

+94
-17
lines changed

12 files changed

+94
-17
lines changed

.changeset/afraid-months-mix.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
'@builder.io/qwik': patch
4+
---
5+
6+
FIX: Error boundary `ErrorBoundary` and fix `useErrorBoundary`

packages/docs/src/routes/api/qwik-city/api.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@
198198
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts",
199199
"mdFile": "qwik-city.documentstyle.md"
200200
},
201+
{
202+
"name": "ErrorBoundary",
203+
"id": "errorboundary",
204+
"hierarchy": [
205+
{
206+
"name": "ErrorBoundary",
207+
"id": "errorboundary"
208+
}
209+
],
210+
"kind": "Variable",
211+
"content": "```typescript\nErrorBoundary: import(\"@builder.io/qwik\").Component<ErrorBoundaryProps>\n```",
212+
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/error-boundary.tsx",
213+
"mdFile": "qwik-city.errorboundary.md"
214+
},
201215
{
202216
"name": "FailOfRest",
203217
"id": "failofrest",

packages/docs/src/routes/api/qwik-city/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,14 @@ string
11321132
11331133
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts)
11341134
1135+
## ErrorBoundary
1136+
1137+
```typescript
1138+
ErrorBoundary: import("@builder.io/qwik").Component<ErrorBoundaryProps>;
1139+
```
1140+
1141+
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/error-boundary.tsx)
1142+
11351143
## FailOfRest
11361144
11371145
```typescript

packages/docs/src/routes/api/qwik/api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3104,7 +3104,7 @@
31043104
}
31053105
],
31063106
"kind": "Function",
3107-
"content": "```typescript\nuseErrorBoundary: () => Readonly<ErrorBoundaryStore>\n```\n**Returns:**\n\nReadonly&lt;[ErrorBoundaryStore](#errorboundarystore)<!-- -->&gt;",
3107+
"content": "```typescript\nuseErrorBoundary: () => ErrorBoundaryStore\n```\n**Returns:**\n\n[ErrorBoundaryStore](#errorboundarystore)",
31083108
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/use/use-error-boundary.ts",
31093109
"mdFile": "qwik.useerrorboundary.md"
31103110
},

packages/docs/src/routes/api/qwik/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10408,12 +10408,12 @@ void
1040810408
## useErrorBoundary
1040910409
1041010410
```typescript
10411-
useErrorBoundary: () => Readonly<ErrorBoundaryStore>;
10411+
useErrorBoundary: () => ErrorBoundaryStore;
1041210412
```
1041310413
1041410414
**Returns:**
1041510415
10416-
Readonly&lt;[ErrorBoundaryStore](#errorboundarystore)&gt;
10416+
[ErrorBoundaryStore](#errorboundarystore)
1041710417
1041810418
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/use/use-error-boundary.ts)
1041910419

packages/qwik-city/src/runtime/src/api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ export interface DocumentStyle {
206206
readonly style: string;
207207
}
208208

209+
// Warning: (ae-forgotten-export) The symbol "ErrorBoundaryProps" needs to be exported by the entry point index.d.ts
210+
//
211+
// @public (undocumented)
212+
export const ErrorBoundary: Component<ErrorBoundaryProps>;
213+
209214
// @public (undocumented)
210215
export type FailOfRest<REST extends readonly DataValidator[]> = REST extends readonly DataValidator<infer ERROR>[] ? ERROR : never;
211216

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import { component$, useErrorBoundary, Slot, type QRL } from '@builder.io/qwik';
1+
import { component$, useErrorBoundary, Slot, type QRL, useOnWindow, $ } from '@builder.io/qwik';
22

33
/** @public */
44
export interface ErrorBoundaryProps {
5-
children: any;
6-
fallback$: QRL<(ev: any) => any>;
5+
fallback$?: QRL<(error: any) => any>;
76
}
87

98
/** @public */
109
export const ErrorBoundary = component$((props: ErrorBoundaryProps) => {
1110
const store = useErrorBoundary();
1211

13-
return store.error === undefined ? <Slot /> : <>{props.fallback$(store.error)}</>;
12+
useOnWindow(
13+
'qerror',
14+
$((e: CustomEvent) => {
15+
store.error = e.detail.error;
16+
})
17+
);
18+
19+
if (store.error && props.fallback$) {
20+
return <>{props.fallback$(store.error)}</>;
21+
}
22+
23+
return <Slot />;
1424
});

packages/qwik-city/src/runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export { server$, serverQrl } from './server-functions';
6464
export { valibot$, valibotQrl } from './server-functions';
6565
export { zod$, zodQrl } from './server-functions';
6666
export { validator$, validatorQrl } from './server-functions';
67+
export { ErrorBoundary } from './error-boundary';
6768

6869
export { z } from 'zod';
6970

packages/qwik/src/core/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ export const useContext: UseContext;
16541654
export const useContextProvider: <STATE>(context: ContextId<STATE>, newValue: STATE) => void;
16551655

16561656
// @public (undocumented)
1657-
export const useErrorBoundary: () => Readonly<ErrorBoundaryStore>;
1657+
export const useErrorBoundary: () => ErrorBoundaryStore;
16581658

16591659
// @public (undocumented)
16601660
export const useId: () => string;
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { qrl } from '../qrl/qrl';
21
import { type ErrorBoundaryStore, ERROR_CONTEXT } from '../render/error-handling';
32
import { useContextProvider } from './use-context';
4-
import { useOn } from './use-on';
53
import { useStore } from './use-store.public';
64

75
/** @public */
8-
export const useErrorBoundary = (): Readonly<ErrorBoundaryStore> => {
9-
const store: ErrorBoundaryStore = useStore({
10-
error: undefined,
11-
});
12-
useOn('error-boundary', qrl('/runtime', 'error', [store]));
13-
useContextProvider(ERROR_CONTEXT, store);
6+
export const useErrorBoundary = () => {
7+
const error = useStore<ErrorBoundaryStore>({ error: undefined });
8+
useContextProvider(ERROR_CONTEXT, error);
149

15-
return store;
10+
return error;
1611
};

0 commit comments

Comments
 (0)