forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorHandler.tsx
154 lines (136 loc) · 4.13 KB
/
ErrorHandler.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {
Box,
Button,
Dialog,
DialogContent,
DialogTitle,
Typography,
} from "@mui/material";
import { styled } from "@mui/material/styles";
import React, { FC, useCallback, useEffect, useState } from "react";
import { ErrorBoundary, FallbackProps } from "react-error-boundary";
import { useLocation, useNavigate } from "react-router-dom";
import { useError } from "react-use";
import { ForbiddenErrorPage } from "./pages/ForbiddenErrorPage";
import { NonTermsServiceAgreementPage } from "./pages/NonTermsServiceAgreement";
import { NotFoundErrorPage } from "./pages/NotFoundErrorPage";
import { toError } from "./services/AironeAPIErrorUtil";
import {
ForbiddenError,
NotFoundError,
NonTermsServiceAgreement,
} from "./services/Exceptions";
import { topPath } from "Routes";
const ErrorDescription = styled(Box)(({ theme }) => ({
marginTop: theme.spacing(2),
marginBottom: theme.spacing(2),
}));
const ErrorDetails = styled(Box)(({ theme }) => ({
marginTop: theme.spacing(1),
marginBottom: theme.spacing(1),
}));
const Buttons = styled(Box)(({ theme }) => ({
marginTop: theme.spacing(2),
marginBottom: theme.spacing(2),
display: "flex",
justifyContent: "flex-end",
}));
interface GenericErrorProps {
children: string;
}
const GenericError: FC<GenericErrorProps> = ({ children }) => {
const navigate = useNavigate();
const [open, setOpen] = useState(true);
const handleGoToTop = useCallback(() => {
navigate(topPath(), { replace: true });
}, [navigate]);
const handleReload = useCallback(() => {
navigate(0);
}, [navigate]);
return (
<Dialog open={open} onClose={() => setOpen(false)}>
<DialogTitle>エラーが発生しました</DialogTitle>
<DialogContent>
<ErrorDescription>
<Typography>
不明なエラーが発生しました。トップページに戻って操作し直してください
</Typography>
<Typography>
エラーが繰り返し発生する場合は管理者にお問い合わせください
</Typography>
</ErrorDescription>
<ErrorDetails>
<Typography variant="body2">エラー詳細: {children}</Typography>
</ErrorDetails>
<Buttons>
<Button
variant="outlined"
color="primary"
onClick={handleGoToTop}
sx={{ mx: 1 }}
>
トップページに戻る
</Button>
<Button
variant="outlined"
color="secondary"
onClick={handleReload}
sx={{ mx: 1 }}
>
リロードする
</Button>
</Buttons>
</DialogContent>
</Dialog>
);
};
const ErrorFallback: FC<FallbackProps> = ({ error, resetErrorBoundary }) => {
const location = useLocation();
useEffect(() => {
resetErrorBoundary();
}, [location, resetErrorBoundary]);
switch (error.name) {
case ForbiddenError.errorName:
return <ForbiddenErrorPage />;
case NotFoundError.errorName:
return <NotFoundErrorPage />;
case NonTermsServiceAgreement.errorName:
return <NonTermsServiceAgreementPage />;
default:
return <GenericError>{error.toString()}</GenericError>;
}
};
const ErrorBridge: FC<{ children: React.ReactNode }> = ({ children }) => {
const dispatchError = useError();
const handleUnhandledRejection = useCallback(
(event: PromiseRejectionEvent) => {
if (event.reason instanceof Response) {
const httpError = toError(event.reason);
if (httpError != null) {
dispatchError(httpError);
}
}
dispatchError(event.reason);
},
[]
);
useEffect(() => {
window.addEventListener("unhandledrejection", handleUnhandledRejection);
return () => {
window.removeEventListener(
"unhandledrejection",
handleUnhandledRejection
);
};
}, [handleUnhandledRejection]);
return <>{children}</>;
};
export const ErrorHandler: FC<{ children: React.ReactNode }> = ({
children,
}) => {
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<ErrorBridge>{children}</ErrorBridge>
</ErrorBoundary>
);
};