@@ -78,7 +78,7 @@ function formatProblem(type, item) {
78
78
/**
79
79
* @typedef {Object } CreateOverlayOptions
80
80
* @property {string | null } trustedTypesPolicyName
81
- * @property {boolean } [catchRuntimeError]
81
+ * @property {boolean | (error: Error) => void } [catchRuntimeError]
82
82
*/
83
83
84
84
/**
@@ -90,6 +90,8 @@ const createOverlay = (options) => {
90
90
let iframeContainerElement ;
91
91
/** @type {HTMLDivElement | null | undefined } */
92
92
let containerElement ;
93
+ /** @type {HTMLDivElement | null | undefined } */
94
+ let headerElement ;
93
95
/** @type {Array<(element: HTMLDivElement) => void> } */
94
96
let onLoadQueue = [ ] ;
95
97
/** @type {TrustedTypePolicy | undefined } */
@@ -124,6 +126,7 @@ const createOverlay = (options) => {
124
126
iframeContainerElement . id = "webpack-dev-server-client-overlay" ;
125
127
iframeContainerElement . src = "about:blank" ;
126
128
applyStyle ( iframeContainerElement , iframeStyle ) ;
129
+
127
130
iframeContainerElement . onload = ( ) => {
128
131
const contentElement =
129
132
/** @type {Document } */
@@ -141,7 +144,7 @@ const createOverlay = (options) => {
141
144
contentElement . id = "webpack-dev-server-client-overlay-div" ;
142
145
applyStyle ( contentElement , containerStyle ) ;
143
146
144
- const headerElement = document . createElement ( "div" ) ;
147
+ headerElement = document . createElement ( "div" ) ;
145
148
146
149
headerElement . innerText = "Compiled with problems:" ;
147
150
applyStyle ( headerElement , headerStyle ) ;
@@ -219,9 +222,15 @@ const createOverlay = (options) => {
219
222
* @param {string } type
220
223
* @param {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }> } messages
221
224
* @param {string | null } trustedTypesPolicyName
225
+ * @param {'build' | 'runtime' } messageSource
222
226
*/
223
- function show ( type , messages , trustedTypesPolicyName ) {
227
+ function show ( type , messages , trustedTypesPolicyName , messageSource ) {
224
228
ensureOverlayExists ( ( ) => {
229
+ headerElement . innerText =
230
+ messageSource === "runtime"
231
+ ? "Uncaught runtime errors:"
232
+ : "Compiled with problems:" ;
233
+
225
234
messages . forEach ( ( message ) => {
226
235
const entryElement = document . createElement ( "div" ) ;
227
236
const msgStyle =
@@ -267,8 +276,8 @@ const createOverlay = (options) => {
267
276
}
268
277
269
278
const overlayService = createOverlayMachine ( {
270
- showOverlay : ( { level = "error" , messages } ) =>
271
- show ( level , messages , options . trustedTypesPolicyName ) ,
279
+ showOverlay : ( { level = "error" , messages, messageSource } ) =>
280
+ show ( level , messages , options . trustedTypesPolicyName , messageSource ) ,
272
281
hideOverlay : hide ,
273
282
} ) ;
274
283
@@ -284,15 +293,22 @@ const createOverlay = (options) => {
284
293
const errorObject =
285
294
error instanceof Error ? error : new Error ( error || message ) ;
286
295
287
- overlayService . send ( {
288
- type : "RUNTIME_ERROR" ,
289
- messages : [
290
- {
291
- message : errorObject . message ,
292
- stack : parseErrorToStacks ( errorObject ) ,
293
- } ,
294
- ] ,
295
- } ) ;
296
+ const shouldDisplay =
297
+ typeof options . catchRuntimeError === "function"
298
+ ? options . catchRuntimeError ( errorObject )
299
+ : true ;
300
+
301
+ if ( shouldDisplay ) {
302
+ overlayService . send ( {
303
+ type : "RUNTIME_ERROR" ,
304
+ messages : [
305
+ {
306
+ message : errorObject . message ,
307
+ stack : parseErrorToStacks ( errorObject ) ,
308
+ } ,
309
+ ] ,
310
+ } ) ;
311
+ }
296
312
} ) ;
297
313
}
298
314
0 commit comments