-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathErrorBoundary.tsx
62 lines (54 loc) · 1.88 KB
/
ErrorBoundary.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
import type { TFunction } from "i18next";
import { Component, type ErrorInfo, type PropsWithChildren } from "react";
import { withTranslation } from "react-i18next";
interface Props {
t: TFunction<[string, string], undefined>;
}
interface State {
hasError: boolean;
error?: Error;
errorInfo?: ErrorInfo;
}
class ErrorBoundary extends Component<PropsWithChildren<Props>, State> {
public state: State = {
hasError: false,
};
public static getDerivedStateFromError(_error: Error): State {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.setState({ ...this.state, errorInfo, error });
console.error("Uncaught error:", error, errorInfo);
}
public render() {
if (this.state.hasError) {
const { t } = this.props;
return (
<main className="flex h-screen w-screen flex-col items-center justify-center gap-5 bg-gray-700 p-10 text-white transition-colors">
<h1 className="text-xl font-bold">{t("login.error")} 😓</h1>
<section>
<p className="rounded bg-red-500 p-2 text-center text-white">
{this.state.error?.name}:{this.state.error?.message}
</p>
<p className="mt-2">
{t("error.report")}{" "}
<a
href="https://github.com/raspiblitz/raspiblitz-web/issues"
className="cursor-pointer text-blue-500"
>
https://github.com/raspiblitz/raspiblitz-web/issues
</a>
</p>
</section>
<section className="md:w-1/2">
<p>{t("error.stack")}:</p>
<p>{this.state.errorInfo?.componentStack}</p>
</section>
</main>
);
}
return this.props.children;
}
}
export default withTranslation()(ErrorBoundary);