-
Notifications
You must be signed in to change notification settings - Fork 307
/
ErrorOverlay.tsx
175 lines (157 loc) · 4.69 KB
/
ErrorOverlay.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as React from "react";
import { useSandpack, useSandpackShell, useErrorMessage } from "../../hooks";
import { css } from "../../styles";
import {
absoluteClassName,
buttonClassName,
errorClassName,
errorBundlerClassName,
errorMessageClassName,
iconStandaloneClassName,
roundedButtonClassName,
} from "../../styles/shared";
import { useClassNames } from "../../utils/classNames";
import { SignInIcon } from "../icons";
import { RestartIcon } from "../icons";
const mapBundlerErrors = (originalMessage: string): string => {
const errorMessage = originalMessage.replace("[sandpack-client]: ", "");
if (/process.exit/.test(errorMessage)) {
const exitCode = errorMessage.match(/process.exit\((\d+)\)/);
if (!exitCode) return errorMessage;
// Crash
if (Number(exitCode[1]) === 0) {
return `Server is not running, would you like to start it again?`;
}
return `Server has crashed with status code ${exitCode[1]}, would you like to restart the server?`;
}
return errorMessage;
};
export type ErrorOverlayProps = React.HTMLAttributes<HTMLDivElement> & {
children?: React.ReactNode;
};
export const ErrorOverlay: React.FC<ErrorOverlayProps> = (props) => {
const { children, className, ...otherProps } = props;
const errorMessage = useErrorMessage();
const { restart } = useSandpackShell();
const classNames = useClassNames();
const {
sandpack: { runSandpack, teamId },
} = useSandpack();
const { dispatch } = useSandpack();
if (!errorMessage && !children) {
return null;
}
const isSandpackBundlerError = errorMessage?.startsWith("[sandpack-client]");
const privateDependencyError = errorMessage?.includes(
"NPM_REGISTRY_UNAUTHENTICATED_REQUEST"
);
const onSignIn = () => {
if (teamId) {
dispatch({ type: "sign-in", teamId });
}
};
if (privateDependencyError) {
return (
<div
className={classNames("overlay", [
classNames("error"),
absoluteClassName,
errorBundlerClassName,
className,
])}
{...props}
>
<p className={classNames("error-message", [errorMessageClassName])}>
<strong>Unable to fetch required dependency.</strong>
</p>
<div className={classNames("error-message", [errorMessageClassName])}>
<p>
Authentication required. Please sign in to your account (make sure
to allow pop-ups to this page) and try again. If the issue persists,
contact{" "}
<a href="mailto:hello@codesandbox.io?subject=Sandpack Timeout Error">
support
</a>{" "}
for further assistance.
</p>
</div>
<div>
<button
className={classNames("button", [
buttonClassName,
iconStandaloneClassName,
roundedButtonClassName,
])}
onClick={onSignIn}
>
<SignInIcon />
<span>Sign in</span>
</button>
</div>
</div>
);
}
if (isSandpackBundlerError && errorMessage) {
return (
<div
className={classNames("overlay", [
classNames("error"),
absoluteClassName,
errorBundlerClassName,
className,
])}
{...otherProps}
>
<div className={classNames("error-message", [errorMessageClassName])}>
<p
className={classNames("error-title", [css({ fontWeight: "bold" })])}
>
Couldn't connect to server
</p>
<p>{mapBundlerErrors(errorMessage)}</p>
<div>
<button
className={classNames("button", [
classNames("icon-standalone"),
buttonClassName,
iconStandaloneClassName,
roundedButtonClassName,
])}
onClick={() => {
restart();
runSandpack();
}}
title="Restart script"
type="button"
>
<RestartIcon /> <span>Restart</span>
</button>
</div>
</div>
</div>
);
}
return (
<div
className={classNames("overlay", [
classNames("error"),
absoluteClassName,
errorClassName({ solidBg: true }),
className,
])}
translate="no"
{...otherProps}
>
<p className={classNames("error-message", [errorMessageClassName])}>
<strong>Something went wrong</strong>
</p>
<p
className={classNames("error-message", [
errorMessageClassName({ errorCode: true }),
])}
>
{errorMessage || children}
</p>
</div>
);
};