Skip to content

Commit 87a26cf

Browse files
authored
feat: catch runtime error (#4605)
1 parent b0f15ac commit 87a26cf

12 files changed

+1634
-1237
lines changed

Diff for: client-src/index.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import webpackHotLog from "webpack/hot/log.js";
44
import stripAnsi from "./utils/stripAnsi.js";
55
import parseURL from "./utils/parseURL.js";
66
import socket from "./socket.js";
7-
import { formatProblem, show, hide } from "./overlay.js";
7+
import { formatProblem, createOverlay } from "./overlay.js";
88
import { log, logEnabledFeatures, setLogLevel } from "./utils/log.js";
99
import sendMessage from "./utils/sendMessage.js";
1010
import reloadApp from "./utils/reloadApp.js";
@@ -115,6 +115,13 @@ self.addEventListener("beforeunload", () => {
115115
status.isUnloading = true;
116116
});
117117

118+
const trustedTypesPolicyName =
119+
typeof options.overlay === "object" && options.overlay.trustedTypesPolicyName;
120+
121+
const overlay = createOverlay({
122+
trustedTypesPolicyName,
123+
});
124+
118125
const onSocketMessage = {
119126
hot() {
120127
if (parsedResourceQuery.hot === "false") {
@@ -135,7 +142,7 @@ const onSocketMessage = {
135142

136143
// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
137144
if (options.overlay) {
138-
hide();
145+
overlay.send({ type: "DISMISS" });
139146
}
140147

141148
sendMessage("Invalid");
@@ -192,7 +199,7 @@ const onSocketMessage = {
192199
log.info("Nothing changed.");
193200

194201
if (options.overlay) {
195-
hide();
202+
overlay.send({ type: "DISMISS" });
196203
}
197204

198205
sendMessage("StillOk");
@@ -201,7 +208,7 @@ const onSocketMessage = {
201208
sendMessage("Ok");
202209

203210
if (options.overlay) {
204-
hide();
211+
overlay.send({ type: "DISMISS" });
205212
}
206213

207214
reloadApp(options, status);
@@ -256,10 +263,11 @@ const onSocketMessage = {
256263
: options.overlay && options.overlay.warnings;
257264

258265
if (needShowOverlayForWarnings) {
259-
const trustedTypesPolicyName =
260-
typeof options.overlay === "object" &&
261-
options.overlay.trustedTypesPolicyName;
262-
show("warning", warnings, trustedTypesPolicyName || null);
266+
overlay.send({
267+
type: "BUILD_ERROR",
268+
level: "warning",
269+
messages: warnings,
270+
});
263271
}
264272

265273
if (params && params.preventReloading) {
@@ -292,10 +300,11 @@ const onSocketMessage = {
292300
: options.overlay && options.overlay.errors;
293301

294302
if (needShowOverlayForErrors) {
295-
const trustedTypesPolicyName =
296-
typeof options.overlay === "object" &&
297-
options.overlay.trustedTypesPolicyName;
298-
show("error", errors, trustedTypesPolicyName || null);
303+
overlay.send({
304+
type: "BUILD_ERROR",
305+
level: "error",
306+
messages: errors,
307+
});
299308
}
300309
},
301310
/**
@@ -308,7 +317,7 @@ const onSocketMessage = {
308317
log.info("Disconnected!");
309318

310319
if (options.overlay) {
311-
hide();
320+
overlay.send({ type: "DISMISS" });
312321
}
313322

314323
sendMessage("Close");

0 commit comments

Comments
 (0)