@octanejs/react-error-boundary 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dominic Gannaway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @octanejs/react-error-boundary
2
+
3
+ Octane adapter for the public `react-error-boundary@6.1.2` API. It uses
4
+ Octane's native `@try`/`@catch` boundary rather than shipping another error
5
+ catching engine.
6
+
7
+ ```tsrx
8
+ import { ErrorBoundary } from '@octanejs/react-error-boundary';
9
+ import { getErrorMessage } from '@octanejs/react-error-boundary';
10
+
11
+ function App() @{
12
+ <ErrorBoundary
13
+ fallbackRender={({ error, resetErrorBoundary }) =>
14
+ <button onClick={() => resetErrorBoundary()}>
15
+ {'Retry after ' + (getErrorMessage(error) ?? 'unknown error')}
16
+ </button>}
17
+ >
18
+ <Screen />
19
+ </ErrorBoundary>
20
+ }
21
+ ```
22
+
23
+ `fallback`, `FallbackComponent`, `fallbackRender`, `onError`, `onReset`,
24
+ `resetKeys`, `ErrorBoundaryContext`, `getErrorMessage`, `useErrorBoundary`, and
25
+ `withErrorBoundary` are supported.
26
+ Errors from event handlers and async callbacks are not render errors; forward
27
+ them with `useErrorBoundary().showBoundary(error)`.
28
+
29
+ For server rendering, import `ErrorBoundary` from
30
+ `@octanejs/react-error-boundary/server`. That entry deliberately does not catch
31
+ descendant errors, matching upstream React server behavior.
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@octanejs/react-error-boundary",
3
+ "version": "0.1.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=22"
8
+ },
9
+ "description": "react-error-boundary for Octane, adapted onto Octane's native error boundary.",
10
+ "author": {
11
+ "name": "Dominic Gannaway",
12
+ "email": "dg@domgan.com"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/octanejs/octane.git",
20
+ "directory": "packages/react-error-boundary"
21
+ },
22
+ "main": "src/index.ts",
23
+ "module": "src/index.ts",
24
+ "types": "src/index.ts",
25
+ "files": [
26
+ "src",
27
+ "README.md"
28
+ ],
29
+ "exports": {
30
+ ".": "./src/index.ts",
31
+ "./server": "./src/server.tsrx"
32
+ },
33
+ "octane": {
34
+ "hookSlots": {
35
+ "manual": [
36
+ "src"
37
+ ]
38
+ }
39
+ },
40
+ "peerDependencies": {
41
+ "octane": "0.1.20"
42
+ },
43
+ "devDependencies": {
44
+ "@tsrx/react": "^0.2.56",
45
+ "esbuild": "^0.28.1",
46
+ "react": "^19.2.7",
47
+ "react-dom": "^19.2.7",
48
+ "react-error-boundary": "6.1.2",
49
+ "vitest": "^4.1.10",
50
+ "octane": "0.1.20"
51
+ },
52
+ "scripts": {
53
+ "test": "vitest run --root ../.. --project react-error-boundary --project react-error-boundary-ssr"
54
+ }
55
+ }
package/src/context.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { createContext } from 'octane';
2
+ import type { ErrorBoundaryContextType } from './types.ts';
3
+
4
+ export const ErrorBoundaryContext = createContext<ErrorBoundaryContextType | null>(null);
@@ -0,0 +1,101 @@
1
+ import { ErrorBoundary as NativeErrorBoundary, useCallback, useLayoutEffect, useRef } from 'octane';
2
+ import type { OctaneNode } from 'octane';
3
+ import { ErrorBoundaryContext } from './context.ts';
4
+ import type { ErrorBoundaryProps, FallbackProps } from './types.ts';
5
+
6
+ export function ErrorBoundary(props: ErrorBoundaryProps) @{
7
+ const nativeResetRef = useRef<(() => void) | null>(null);
8
+ const caughtRef = useRef(false);
9
+ const propsRef = useRef(props);
10
+ propsRef.current = props;
11
+ caughtRef.current = false;
12
+ const resetErrorBoundary = useCallback((...args: unknown[]) => {
13
+ if (!caughtRef.current) return;
14
+ propsRef.current.onReset?.({ args, reason: 'imperative-api' });
15
+ caughtRef.current = false;
16
+ nativeResetRef.current?.();
17
+ }, []);
18
+ const idleContext = {
19
+ didCatch: false,
20
+ error: null,
21
+ resetErrorBoundary,
22
+ };
23
+
24
+ <NativeErrorBoundary
25
+ resetRef={nativeResetRef}
26
+ fallback={(error: unknown) =>
27
+ <CaughtError
28
+ error={error}
29
+ reset={nativeResetRef.current!}
30
+ resetErrorBoundary={resetErrorBoundary}
31
+ caughtRef={caughtRef}
32
+ propsRef={propsRef}
33
+ />}
34
+ >
35
+ <ErrorBoundaryContext.Provider
36
+ value={idleContext}
37
+ >{props.children}</ErrorBoundaryContext.Provider>
38
+ </NativeErrorBoundary>
39
+ }
40
+
41
+ function CaughtError(props: {
42
+ error: unknown;
43
+ reset: () => void;
44
+ resetErrorBoundary: (...args: unknown[]) => void;
45
+ caughtRef: { current: boolean };
46
+ propsRef: { current: ErrorBoundaryProps };
47
+ }) @{
48
+ props.caughtRef.current = true;
49
+ const unreported = useRef(true);
50
+ useLayoutEffect(() => {
51
+ if (unreported.current) {
52
+ unreported.current = false;
53
+ props.propsRef.current.onError?.(props.error, { componentStack: '' });
54
+ }
55
+ }, [props.error]);
56
+
57
+ const resetKeys = props.propsRef.current.resetKeys;
58
+ const previousResetKeys = useRef(resetKeys);
59
+
60
+ useLayoutEffect(() => {
61
+ const previous = previousResetKeys.current;
62
+ if (hasArrayChanged(previous, resetKeys)) {
63
+ previousResetKeys.current = resetKeys;
64
+ props.propsRef.current.onReset?.({
65
+ next: resetKeys,
66
+ prev: previous,
67
+ reason: 'keys',
68
+ });
69
+ props.caughtRef.current = false;
70
+ props.reset();
71
+ }
72
+ }, [resetKeys]);
73
+
74
+ const fallbackProps: FallbackProps = {
75
+ error: props.error,
76
+ resetErrorBoundary: props.resetErrorBoundary,
77
+ };
78
+ const context = {
79
+ didCatch: true,
80
+ error: props.error,
81
+ resetErrorBoundary: props.resetErrorBoundary,
82
+ };
83
+ const boundaryProps = props.propsRef.current;
84
+ const FallbackComponent = boundaryProps.FallbackComponent;
85
+ let fallback: OctaneNode;
86
+ if (boundaryProps.fallbackRender) {
87
+ fallback = boundaryProps.fallbackRender(fallbackProps);
88
+ } else if (FallbackComponent) {
89
+ fallback = <FallbackComponent {...fallbackProps} />;
90
+ } else if (boundaryProps.fallback !== undefined) {
91
+ fallback = boundaryProps.fallback;
92
+ } else {
93
+ throw props.error;
94
+ }
95
+
96
+ <ErrorBoundaryContext.Provider value={context}>{fallback}</ErrorBoundaryContext.Provider>
97
+ }
98
+
99
+ function hasArrayChanged(a: unknown[] = [], b: unknown[] = []): boolean {
100
+ return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
101
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ export { ErrorBoundary } from './error-boundary.tsrx';
2
+ export { ErrorBoundaryContext } from './context.ts';
3
+ export { useErrorBoundary } from './use-error-boundary.ts';
4
+ export { withErrorBoundary } from './with-error-boundary.tsrx';
5
+ export { getErrorMessage } from './utils.ts';
6
+ export type {
7
+ ComponentType,
8
+ ErrorBoundaryContextType,
9
+ ErrorBoundaryProps,
10
+ ErrorBoundaryPropsWithComponent,
11
+ ErrorBoundaryPropsWithFallback,
12
+ ErrorBoundaryPropsWithRender,
13
+ FallbackProps,
14
+ OnErrorCallback,
15
+ } from './types.ts';
16
+ export type { UseErrorBoundaryApi } from './use-error-boundary.ts';
@@ -0,0 +1,21 @@
1
+ const subSlotCache = new Map<symbol, Map<string, symbol>>();
2
+ const bareSlotCache = new Map<string, symbol>();
3
+
4
+ export function subSlot(slot: symbol | undefined, tag: string): symbol {
5
+ if (slot === undefined) {
6
+ let bare = bareSlotCache.get(tag);
7
+ if (bare === undefined)
8
+ bareSlotCache.set(tag, (bare = Symbol.for('react-error-boundary:' + tag)));
9
+ return bare;
10
+ }
11
+ let byTag = subSlotCache.get(slot);
12
+ if (byTag === undefined) subSlotCache.set(slot, (byTag = new Map()));
13
+ let derived = byTag.get(tag);
14
+ if (derived === undefined) {
15
+ byTag.set(
16
+ tag,
17
+ (derived = Symbol.for((slot.description ?? 'react-error-boundary') + ':' + tag)),
18
+ );
19
+ }
20
+ return derived;
21
+ }
@@ -0,0 +1,33 @@
1
+ import type { OctaneNode } from 'octane';
2
+ import { ErrorBoundaryContext } from './context.ts';
3
+ import type { ComponentType, ErrorBoundaryProps } from './types.ts';
4
+
5
+ // Upstream react-error-boundary does not catch errors during server rendering.
6
+ // This explicit server entry therefore renders children without installing
7
+ // Octane's native server boundary.
8
+ export function ErrorBoundary(props: ErrorBoundaryProps) @{
9
+ <ErrorBoundaryContext.Provider
10
+ value={{ didCatch: false, error: null, resetErrorBoundary: () => {} }}
11
+ >{props.children as OctaneNode}</ErrorBoundaryContext.Provider>
12
+ }
13
+
14
+ export function withErrorBoundary<Props extends object>(
15
+ BaseComponent: ComponentType<Props>,
16
+ errorBoundaryProps: ErrorBoundaryProps,
17
+ ): ComponentType<Props> {
18
+ function WrappedComponent(props: Props) {
19
+ return <ErrorBoundary {...errorBoundaryProps}>
20
+ <BaseComponent {...props} />
21
+ </ErrorBoundary>;
22
+ }
23
+ WrappedComponent.displayName = `withErrorBoundary(${
24
+ BaseComponent.displayName || BaseComponent.name || 'Unknown'
25
+ })`;
26
+ return WrappedComponent;
27
+ }
28
+
29
+ export { useErrorBoundary } from './use-error-boundary.ts';
30
+ export { ErrorBoundaryContext };
31
+ export { getErrorMessage } from './utils.ts';
32
+ export type { ComponentType, ErrorBoundaryContextType, ErrorBoundaryProps, ErrorBoundaryPropsWithComponent, ErrorBoundaryPropsWithFallback, ErrorBoundaryPropsWithRender, FallbackProps, OnErrorCallback } from './types.ts';
33
+ export type { UseErrorBoundaryApi } from './use-error-boundary.ts';
package/src/types.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type { ComponentBody, OctaneNode } from 'octane';
2
+
3
+ export type FallbackProps = {
4
+ error: unknown;
5
+ resetErrorBoundary: (...args: unknown[]) => void;
6
+ };
7
+
8
+ export type OnErrorCallback = (error: unknown, info: { componentStack: string }) => void;
9
+
10
+ type ErrorBoundarySharedProps = {
11
+ children?: OctaneNode;
12
+ onError?: OnErrorCallback;
13
+ onReset?: (
14
+ details:
15
+ | { args: unknown[]; reason: 'imperative-api' }
16
+ | { next: unknown[] | undefined; prev: unknown[] | undefined; reason: 'keys' },
17
+ ) => void;
18
+ resetKeys?: unknown[];
19
+ };
20
+
21
+ export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
22
+ fallback?: never;
23
+ FallbackComponent: ComponentBody<FallbackProps>;
24
+ fallbackRender?: never;
25
+ };
26
+
27
+ export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
28
+ fallback?: never;
29
+ FallbackComponent?: never;
30
+ fallbackRender: (props: FallbackProps) => OctaneNode;
31
+ };
32
+
33
+ export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
34
+ fallback: OctaneNode;
35
+ FallbackComponent?: never;
36
+ fallbackRender?: never;
37
+ };
38
+
39
+ export type ErrorBoundaryProps =
40
+ ErrorBoundaryPropsWithFallback | ErrorBoundaryPropsWithComponent | ErrorBoundaryPropsWithRender;
41
+
42
+ export type ErrorBoundaryContextType = {
43
+ didCatch: boolean;
44
+ error: unknown | null;
45
+ resetErrorBoundary: (...args: unknown[]) => void;
46
+ };
47
+
48
+ export type ComponentType<P = {}> = ComponentBody<P> & {
49
+ displayName?: string;
50
+ name?: string;
51
+ };
@@ -0,0 +1,36 @@
1
+ import { useContext, useMemo, useState } from 'octane';
2
+ import { ErrorBoundaryContext } from './context.ts';
3
+ import { subSlot } from './internal.ts';
4
+
5
+ export type UseErrorBoundaryApi = {
6
+ error: unknown | null;
7
+ resetBoundary: () => void;
8
+ showBoundary: (error: unknown) => void;
9
+ };
10
+
11
+ export function useErrorBoundary(): UseErrorBoundaryApi;
12
+ export function useErrorBoundary(slot?: symbol): UseErrorBoundaryApi {
13
+ const context = useContext(ErrorBoundaryContext);
14
+ if (context === null) {
15
+ throw new Error('ErrorBoundaryContext not found');
16
+ }
17
+
18
+ const [state, setState] = useState<
19
+ { error: unknown; hasError: true } | { error: null; hasError: false }
20
+ >({ error: null, hasError: false }, subSlot(slot, 'state'));
21
+ const api = useMemo(
22
+ () => ({
23
+ error: context.error,
24
+ resetBoundary: () => {
25
+ context.resetErrorBoundary();
26
+ setState({ error: null, hasError: false });
27
+ },
28
+ showBoundary: (error: unknown) => setState({ error, hasError: true }),
29
+ }),
30
+ [context.error, context.resetErrorBoundary],
31
+ subSlot(slot, 'api'),
32
+ );
33
+
34
+ if (state.hasError) throw state.error;
35
+ return api;
36
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,12 @@
1
+ export function getErrorMessage(thrown: unknown): string | undefined {
2
+ if (typeof thrown === 'string') return thrown;
3
+ if (
4
+ thrown !== null &&
5
+ typeof thrown === 'object' &&
6
+ 'message' in thrown &&
7
+ typeof thrown.message === 'string'
8
+ ) {
9
+ return thrown.message;
10
+ }
11
+ return undefined;
12
+ }
@@ -0,0 +1,18 @@
1
+ import type { ComponentType, ErrorBoundaryProps } from './types.ts';
2
+ import { ErrorBoundary } from './error-boundary.tsrx';
3
+
4
+ export function withErrorBoundary<Props extends object>(
5
+ BaseComponent: ComponentType<Props>,
6
+ errorBoundaryProps: ErrorBoundaryProps,
7
+ ): ComponentType<Props> {
8
+ function WrappedComponent(props: Props) {
9
+ return <ErrorBoundary {...errorBoundaryProps}>
10
+ <BaseComponent {...props} />
11
+ </ErrorBoundary>;
12
+ }
13
+
14
+ WrappedComponent.displayName = `withErrorBoundary(${
15
+ BaseComponent.displayName || BaseComponent.name || 'Unknown'
16
+ })`;
17
+ return WrappedComponent;
18
+ }