-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror.tsx
146 lines (131 loc) · 4.13 KB
/
error.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { HttpError, HttpErrorOptions, isHttpError } from "x/http_error/mod.ts";
import {
ComponentType,
createContext,
isValidElement,
PropsWithChildren,
ReactElement,
useContext,
useEffect,
useState,
} from "npm/react";
import { useLocation } from "npm/react-router-dom";
import { ErrorBoundary } from "npm/react-error-boundary";
import type {
ErrorBoundaryProps,
FallbackProps,
} from "npm/react-error-boundary";
export { HttpError, isHttpError };
export type { ErrorBoundaryProps, FallbackProps, HttpErrorOptions };
/**
* For internal use only.
* Used to provider errors on the server to the browser.
*/
export const AppErrorContext = createContext<{ error?: HttpError }>({});
export type AppErrorBoundaryProps = ErrorBoundaryProps & {
/** Used to associate errors on the server side with the boundary when using server side rendering. */
boundary?: string;
};
/**
* Any errors within the boundary will be captured by it.
* Unlike ErrorBoundary, the AppErrorBoundary can be used to render errors on the server.
* For the error on the server to be caught by it, it must have the same boundary.
*/
export function AppErrorBoundary(
props: PropsWithChildren<AppErrorBoundaryProps>,
) {
const { children, boundary, ...errorBoundaryProps } = props;
const errorContext = useContext(AppErrorContext);
const initialError = errorContext.error ?? null;
const [error, setError] = useState(initialError);
useEffect(() => {
return () => {
if (error) setError(null);
};
}, []);
if (!error || boundary !== error.data.boundary) {
return (
<ErrorBoundary
{...errorBoundaryProps}
>
{children}
</ErrorBoundary>
);
}
delete errorContext.error;
const fallbackProps = {
error,
resetErrorBoundary: (...args: unknown[]) => {
setError(null);
errorBoundaryProps.onReset?.(...args);
},
} as FallbackProps;
const { fallback, fallbackRender, FallbackComponent } = errorBoundaryProps;
if (isValidElement(fallback)) {
return fallback;
} else if (typeof fallbackRender === "function") {
return fallbackRender(fallbackProps);
} else if (FallbackComponent) {
return <FallbackComponent {...fallbackProps} />;
} else {
throw new Error(
"AppErrorBoundary requires either a fallback, fallbackRender, or FallbackComponent prop.",
);
}
}
/**
* Wraps a component with an AppErrorBoundary.
* Any errors within the boundary will be captured by it.
* Unlike withErrorBoundary, withAppErrorBoundary can be used to render errors on the server.
* For the error on the server to be caught by it, it must have the same boundary.
*/
export function withAppErrorBoundary<P>(
Component: ComponentType<P>,
errorBoundaryProps: AppErrorBoundaryProps,
): ComponentType<P> {
const Wrapped = ((props) => {
return (
<AppErrorBoundary {...errorBoundaryProps}>
<Component
{
// deno-lint-ignore no-explicit-any
...props as any
}
/>
</AppErrorBoundary>
);
}) as ComponentType<P>;
// Format for display in DevTools
const name = Component.displayName || Component.name || "Unknown";
Wrapped.displayName = `withAppErrorBoundary(${name})`;
return Wrapped;
}
/**
* A simple error fallback that will show the error and provide a button for trying again.
* The error will clear when clicking the try again button or navigating to a different route.
*/
export function DefaultErrorFallback(
{ error, resetErrorBoundary }: FallbackProps,
) {
const location = useLocation();
const [initialLocation] = useState(location);
useEffect(() => {
if (location !== initialLocation) resetErrorBoundary();
}, [location]);
return (
<div role="alert">
<p>Something went wrong:</p>
<pre>{error.message}</pre>
{isHttpError(error) && error.status !== 404
? <button onClick={resetErrorBoundary}>Try again</button>
: null}
</div>
);
}
/**
* This component can be used to throw a 404 not found error.
* It's used as the default wildcard route at the top level of your app.
*/
export function NotFound(): ReactElement {
throw new HttpError(404, "Not found");
}