-
Notifications
You must be signed in to change notification settings - Fork 12
/
Error.jsx
318 lines (288 loc) · 11.4 KB
/
Error.jsx
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (C) 2023 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import React, { useCallback, useContext, useEffect, useState } from "react";
import {
ActionList,
Button,
Form,
FormGroup,
FormHelperText,
HelperText,
HelperTextItem,
Modal,
ModalVariant,
Stack,
StackItem,
Text,
TextArea,
TextContent,
TextVariants,
} from "@patternfly/react-core";
import { DisconnectedIcon, ExternalLinkAltIcon } from "@patternfly/react-icons";
import { setCriticalErrorAction, setCriticalErrorFrontendAction } from "../actions/miscellaneous-actions.js";
import { exitGui } from "../helpers/exit.js";
import { SystemTypeContext } from "./Common.jsx";
import "./Error.scss";
const _ = cockpit.gettext;
export const bugzillaPrefiledReportURL = (productQueryData) => {
const baseURL = "https://bugzilla.redhat.com";
const queryData = {
...productQueryData,
component: "anaconda",
};
const reportURL = new URL(baseURL);
reportURL.pathname = "enter_bug.cgi";
Object.keys(queryData).map(query => reportURL.searchParams.append(query, queryData[query]));
return reportURL.href;
};
const ensureMaximumReportURLLength = (reportURL) => {
const newUrl = new URL(reportURL);
// The current limit on URL length is 8KiB server limit.
const searchParamsLimits = [
// Summary should be short
{ length: 256, param: "short_desc" },
// We reserve some space in Details text for attachment message which
// will be always appended to the end.
{ length: 8192 - 256 - 100, param: "comment" },
];
const sp = newUrl.searchParams;
searchParamsLimits.forEach((limit) => {
if (sp.get(limit.param)?.length > limit.length) {
sp.set(limit.param, sp.get(limit.param).slice(0, limit.length));
}
});
return newUrl.href;
};
const addLogAttachmentCommentToReportURL = (reportURL, logFile) => {
const newUrl = new URL(reportURL);
const comment = newUrl.searchParams.get("comment") || "";
newUrl.searchParams.set("comment", comment +
"\n\n" + cockpit.format(_("Please attach the log file $0 to the issue."), logFile));
return newUrl.href;
};
export const BZReportModal = ({
buttons,
description,
detailsContent,
detailsLabel,
idPrefix,
isConnected,
logFile,
reportLinkURL,
title,
titleIconVariant
}) => {
const [logContent, setLogContent] = useState();
const [preparingReport, setPreparingReport] = useState(false);
useEffect(() => {
cockpit.spawn(["journalctl", "-a"])
.then(content => setLogContent(content));
}, []);
const openBZIssue = (reportURL, logFile, logContent) => {
reportURL = ensureMaximumReportURLLength(reportURL);
reportURL = addLogAttachmentCommentToReportURL(reportURL, logFile);
setPreparingReport(true);
cockpit
.file(logFile)
.replace(logContent)
.always(() => setPreparingReport(false))
.then(() => window.open(reportURL, "_blank", "noopener,noreferer"));
};
const isBootIso = useContext(SystemTypeContext) === "BOOT_ISO";
const networkHelperMessageLive = _("Network not available. Configure the network in the top bar menu to report the issue.");
const networkHelperMessageBootIso = _("Network not available. Configure the network to report the issue.");
return (
<Modal
description={description}
id={idPrefix + "-bz-report-modal"}
isOpen
position="top"
showClose={false}
title={title}
titleIconVariant={titleIconVariant}
variant={ModalVariant.large}
footer={
<Stack hasGutter>
<FormHelperText isHidden={false}>
<HelperText>
{isConnected
? <HelperTextItem> {_("Reporting an issue will send information over the network. Please review and edit the attached log to remove any sensitive information.")} </HelperTextItem>
: <HelperTextItem icon={<DisconnectedIcon />}> {isBootIso ? networkHelperMessageBootIso : networkHelperMessageLive} </HelperTextItem>}
</HelperText>
</FormHelperText>
<StackItem>
<ActionList>
<Button
variant="primary"
isLoading={preparingReport}
isDisabled={logContent === undefined || preparingReport || !isConnected}
icon={<ExternalLinkAltIcon />}
onClick={() => openBZIssue(reportLinkURL, logFile, logContent)}
component="a">
{preparingReport ? _("Preparing report") : _("Report issue")}
</Button>
{buttons}
</ActionList>
</StackItem>
</Stack>
}>
<Form>
{detailsLabel &&
<FormGroup
fieldId={idPrefix + "-bz-report-modal-details"}
label={detailsLabel}
>
{detailsContent}
</FormGroup>}
<FormGroup
fieldId={idPrefix + "-bz-report-modal-review-log"}
label={_("Log")}
>
<TextArea
value={logContent}
onChange={(_, value) => setLogContent(value)}
resizeOrientation="vertical"
id={idPrefix + "-bz-report-modal-review-log"}
isDisabled={logContent === undefined || preparingReport}
rows={25}
/>
</FormGroup>
</Form>
</Modal>
);
};
const addExceptionDataToReportURL = (url, exception) => {
const newUrl = new URL(url);
const backendMessage = exception.backendException?.message || "";
const frontendMessage = exception.frontendException?.message || "";
const bothSeparator = exception.backendException?.message && exception.frontendException?.message ? "\n" : "";
const context = exception.contextData?.context ? exception.contextData.context + " " : "";
const name = exception.backendException?.name ? exception.backendException.name + ": " : "";
const stackTrace = exception.frontendException?.stack ? "\n\nStackTrace: " + exception.frontendException.stack : "";
newUrl.searchParams.append(
"short_desc",
"WebUI: " + context + name + backendMessage + frontendMessage
);
newUrl.searchParams.append(
"comment",
"Installer WebUI Critical Error:\n" + context + name + backendMessage + bothSeparator + frontendMessage + stackTrace
);
return newUrl.href;
};
const exceptionInfo = (exception, idPrefix) => {
const exceptionNamePrefix = exception.backendException?.name ? exception.backendMessage?.name + ": " : "";
const backendMessage = exception.backendException?.message ? exception.backendException.message : "";
const frontendMessage = exception.frontendException?.message ? exception.frontendException.message : "";
return (
<TextContent id={idPrefix + "-bz-report-modal-details"}>
{backendMessage &&
<Text component={TextVariants.p}>
{exceptionNamePrefix + backendMessage}
</Text>}
{frontendMessage &&
<Text component={TextVariants.p}>
{exceptionNamePrefix + frontendMessage}
</Text>}
</TextContent>
);
};
const quitButton = (isBootIso) => {
return (
<Button variant="secondary" onClick={exitGui} key="reboot">
{isBootIso ? _("Reboot") : _("Quit")}
</Button>
);
};
export const CriticalError = ({ exception, isConnected, reportLinkURL }) => {
const isBootIso = useContext(SystemTypeContext) === "BOOT_ISO";
const context = exception.backendException?.contextData?.context || exception.frontendException?.jsContext;
const description = context
? cockpit.format(_("The installer cannot continue due to a critical error: $0"), _(context))
: _("The installer cannot continue due to a critical error.");
const idPrefix = "critical-error";
return (
<BZReportModal
description={description}
reportLinkURL={addExceptionDataToReportURL(reportLinkURL, exception)}
idPrefix={idPrefix}
title={_("Critical error")}
titleIconVariant="danger"
logFile="/tmp/webui.log"
detailsLabel={_("Error details")}
detailsContent={exceptionInfo(exception, idPrefix)}
buttons={[quitButton(isBootIso)]}
isConnected={isConnected}
/>
);
};
const cancelButton = (onClose) => {
return (
<Button variant="link" onClick={() => onClose()} id="user-issue-dialog-cancel-btn" key="cancel">
{_("Cancel")}
</Button>
);
};
export const UserIssue = ({ isConnected, reportLinkURL, setIsReportIssueOpen }) => {
return (
<BZReportModal
description={_("The following log will be sent to the issue tracking system where you may provide additional details.")}
reportLinkURL={reportLinkURL}
idPrefix="user-issue"
title={_("Report issue")}
titleIconVariant={null}
logFile="/tmp/webui.log"
buttons={[cancelButton(() => setIsReportIssueOpen(false))]}
isConnected={isConnected}
/>
);
};
const errorHandlerWithContext = (contextData, handler) => {
return (exception) => {
exception.contextData = contextData;
handler(exception);
};
};
export const useError = ({ dispatch }) => {
useEffect(() => {
// Listen on JS errors
window.onerror = (message, url, line, col, errObj) => {
dispatch(setCriticalErrorFrontendAction(errObj));
};
// Listen to JS errors from async operations
const handleUnhandledRejection = (event) => {
dispatch(setCriticalErrorFrontendAction(event.reason));
};
window.addEventListener("unhandledrejection", handleUnhandledRejection);
return () => {
window.removeEventListener("unhandledrejection", handleUnhandledRejection);
};
}, [dispatch]);
const onCritFail = useCallback((contextData) => {
return errorHandlerWithContext(
contextData,
exc => {
if (contextData.isFrontend) {
dispatch(setCriticalErrorFrontendAction(exc));
} else {
dispatch(setCriticalErrorAction(exc));
}
}
);
}, [dispatch]);
return onCritFail;
};