-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-boundary.tsx
More file actions
40 lines (36 loc) · 1.13 KB
/
error-boundary.tsx
File metadata and controls
40 lines (36 loc) · 1.13 KB
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
import React from "react";
import { isRouteErrorResponse, useParams, useRouteError, type ErrorResponse } from "react-router";
import { getErrorMessage } from "~/utils/misc";
type StatusHandler = (info: {
error: ErrorResponse;
params: Record<string, string | undefined>;
}) => React.JSX.Element | null;
export function GeneralErrorBoundary({
defaultStatusHandler = ({ error }) => (
<p>
{error.status} {error.data}
</p>
),
statusHandlers,
unexpectedErrorHandler = (error) => <p>{getErrorMessage(error)}</p>,
}: {
defaultStatusHandler?: StatusHandler;
statusHandlers?: Record<number, StatusHandler>;
unexpectedErrorHandler?: (error: unknown) => React.JSX.Element | null;
}) {
const error = useRouteError();
const params = useParams();
if (typeof document !== "undefined") {
console.error(error);
}
return (
<div className="text-h2 container mx-auto flex items-center justify-center p-20">
{isRouteErrorResponse(error)
? (statusHandlers?.[error.status] ?? defaultStatusHandler)({
error,
params,
})
: unexpectedErrorHandler(error)}
</div>
);
}