-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy patherror.ts
55 lines (49 loc) · 1.33 KB
/
error.ts
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
import type { FC, PropsWithChildren } from "react";
import { Component, createElement } from "react";
type ErrorBoundaryProps = PropsWithChildren<{ Handler: FC<{ error: Error }> }>;
export class ErrorBoundary extends Component<ErrorBoundaryProps, { error: Error | null }> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { error: null };
}
static getDerivedStateFromError(error: Error) {
return { error };
}
render() {
if (this.state.error instanceof Error) {
return createElement(this.props.Handler, { error: this.state.error });
}
return this.props.children;
}
}
export function Err({
error: { status, message },
fullscreen,
}: {
error: { status?: number; message: string };
fullscreen?: boolean;
}) {
return createElement(
"div",
{
style: fullscreen
? {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100vw",
height: "100vh",
fontSize: 18,
}
: {
margin: "0",
padding: "1.5rem 2rem",
color: "red",
fontSize: 18,
},
},
status && createElement("strong", { style: { fontWeight: "600" } }, status),
status && createElement("small", { style: { opacity: 0.5, padding: "0 6px" } }, "-"),
message,
);
}