2022-02-25 21:23:16 +01:00
|
|
|
import { IconPatch } from "components/buttons/IconPatch";
|
2022-02-18 14:51:30 +01:00
|
|
|
import { Icons } from "components/Icon";
|
2022-02-25 21:23:16 +01:00
|
|
|
import { Link } from "components/text/Link";
|
|
|
|
import { Title } from "components/text/Title";
|
2022-02-25 21:20:35 +01:00
|
|
|
import { DISCORD_LINK, GITHUB_LINK } from "mw_constants";
|
2022-02-16 21:30:12 +01:00
|
|
|
import { Component } from "react";
|
|
|
|
|
|
|
|
interface ErrorBoundaryState {
|
|
|
|
hasError: boolean;
|
|
|
|
error?: {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
path: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-06 14:41:51 +01:00
|
|
|
export class ErrorBoundary extends Component<
|
|
|
|
Record<string, unknown>,
|
|
|
|
ErrorBoundaryState
|
|
|
|
> {
|
|
|
|
constructor() {
|
|
|
|
super({});
|
|
|
|
this.state = {
|
|
|
|
hasError: false,
|
|
|
|
};
|
|
|
|
}
|
2022-02-16 21:30:12 +01:00
|
|
|
|
|
|
|
static getDerivedStateFromError() {
|
|
|
|
return {
|
|
|
|
hasError: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error: any, errorInfo: any) {
|
|
|
|
console.error("Render error caught", error, errorInfo);
|
|
|
|
if (error instanceof Error) {
|
2022-03-06 13:43:32 +01:00
|
|
|
const realError: Error = error as Error;
|
2022-02-16 21:30:12 +01:00
|
|
|
this.setState((s) => ({
|
|
|
|
...s,
|
|
|
|
hasError: true,
|
|
|
|
error: {
|
|
|
|
name: realError.name,
|
|
|
|
description: realError.message,
|
|
|
|
path: errorInfo.componentStack.split("\n")[1],
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state.hasError) return this.props.children;
|
|
|
|
|
|
|
|
return (
|
2022-02-18 14:51:30 +01:00
|
|
|
<div className="flex min-h-screen w-full flex-col items-center justify-center px-4 py-12">
|
|
|
|
<div className="flex flex-col items-center justify-start text-center">
|
|
|
|
<IconPatch icon={Icons.WARNING} className="mb-6 text-red-400" />
|
2022-02-16 21:30:12 +01:00
|
|
|
<Title>Whoops, it broke</Title>
|
2022-02-18 14:51:30 +01:00
|
|
|
<p className="my-6 max-w-lg">
|
2022-03-06 14:41:51 +01:00
|
|
|
The app encountered an error and wasn't able to recover, please
|
|
|
|
report it to the{" "}
|
|
|
|
<Link url={DISCORD_LINK} newTab>
|
|
|
|
Discord server
|
|
|
|
</Link>{" "}
|
|
|
|
or on{" "}
|
|
|
|
<Link url={GITHUB_LINK} newTab>
|
|
|
|
GitHub
|
|
|
|
</Link>
|
|
|
|
.
|
2022-02-16 21:30:12 +01:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
{this.state.error ? (
|
2022-02-18 14:55:22 +01:00
|
|
|
<div className="bg-denim-300 w-4xl mt-12 max-w-full rounded px-6 py-4">
|
|
|
|
<p className="mb-1 break-words font-bold text-white">
|
2022-02-16 21:30:12 +01:00
|
|
|
{this.state.error.name} - {this.state.error.description}
|
|
|
|
</p>
|
2022-02-18 14:51:30 +01:00
|
|
|
<p className="break-words">{this.state.error.path}</p>
|
2022-02-16 21:30:12 +01:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|