forked from aep-dev/aep-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_boundary.tsx
More file actions
99 lines (84 loc) · 2.64 KB
/
Copy patherror_boundary.tsx
File metadata and controls
99 lines (84 loc) · 2.64 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
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
import { Component, ErrorInfo, ReactNode } from "react";
import { useRouteError, useNavigate } from "react-router-dom";
import { findErrorHandler } from "@/lib/error_handlers";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
interface ErrorDisplayProps {
error: Error | unknown;
reset: () => void;
}
export function ErrorDisplay({ error, reset }: ErrorDisplayProps) {
const navigate = useNavigate();
const errorMessage = (error as Error)?.message || (typeof error === 'string' ? error : "Something went wrong.");
let title = "An error occurred";
let description = errorMessage;
let action = <Button onClick={reset}>Go Home</Button>;
const handler = findErrorHandler(error);
if (handler) {
title = handler.title(error);
description = handler.description(error);
const actionContent = handler.action(error, { error, reset, navigate });
if (actionContent) {
action = <>{actionContent}</>;
}
}
return (
<Dialog open={true} onOpenChange={() => reset()}>
<DialogContent>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
<DialogDescription>
{description}
</DialogDescription>
</DialogHeader>
<DialogFooter>
{action}
</DialogFooter>
</DialogContent>
</Dialog>
);
}
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("Uncaught error:", error, errorInfo);
}
private handleClose = () => {
this.setState({ hasError: false, error: null });
window.location.href = "/";
};
public render() {
if (this.state.hasError) {
return <ErrorDisplay error={this.state.error} reset={this.handleClose} />;
}
return this.props.children;
}
}
export function RouteErrorBoundary() {
const error = useRouteError();
const navigate = useNavigate();
const handleClose = () => {
navigate("/");
};
return <ErrorDisplay error={error} reset={handleClose} />;
}