Skip to content

Commit e266085

Browse files
committed
reportTheException now accepts unknown instead of only Error.
1 parent a6918b6 commit e266085

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

packages/custom-elements/ts_src/CustomElementInternals.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export default class CustomElementInternals {
281281
this._upgradeAnElement(element, definition);
282282
}
283283
} catch (e: unknown) {
284-
this.reportTheException(e as Error);
284+
this.reportTheException(e);
285285
}
286286
}
287287

@@ -341,7 +341,7 @@ export default class CustomElementInternals {
341341
try {
342342
definition.connectedCallback.call(element);
343343
} catch (e: unknown) {
344-
this.reportTheException(e as Error);
344+
this.reportTheException(e);
345345
}
346346
}
347347
}
@@ -352,7 +352,7 @@ export default class CustomElementInternals {
352352
try {
353353
definition.disconnectedCallback.call(element);
354354
} catch (e: unknown) {
355-
this.reportTheException(e as Error);
355+
this.reportTheException(e);
356356
}
357357
}
358358
}
@@ -378,7 +378,7 @@ export default class CustomElementInternals {
378378
namespace
379379
);
380380
} catch (e: unknown) {
381-
this.reportTheException(e as Error);
381+
this.reportTheException(e);
382382
}
383383
}
384384
}
@@ -506,7 +506,7 @@ export default class CustomElementInternals {
506506

507507
return result;
508508
} catch (e: unknown) {
509-
this.reportTheException(e as Error);
509+
this.reportTheException(e);
510510

511511
// When construction fails, a new HTMLUnknownElement is produced.
512512
// However, there's no direct way to create one, so we create a
@@ -537,7 +537,7 @@ export default class CustomElementInternals {
537537
*
538538
* @see https://html.spec.whatwg.org/multipage/webappapis.html#report-the-exception
539539
*/
540-
reportTheException(errorArg: Error) {
540+
reportTheException(errorArg: unknown) {
541541
interface ExtendedError extends Error {
542542
// Non-standard Safari properties.
543543
sourceURL?: string;
@@ -550,11 +550,26 @@ export default class CustomElementInternals {
550550
columnNumber?: number;
551551
}
552552

553-
const error = errorArg as ExtendedError;
554-
const message = error.message;
555-
const filename = error.sourceURL || error.fileName || '';
556-
const lineno = error.line || error.lineNumber || 0;
557-
const colno = error.column || error.columnNumber || 0;
553+
const getErrorInfo = (maybeError: unknown) => {
554+
if (maybeError instanceof Error) {
555+
const error = maybeError as ExtendedError;
556+
return {
557+
message: error.message,
558+
filename: error.sourceURL || error.fileName || '',
559+
lineno: error.line || error.lineNumber || 0,
560+
colno: error.column || error.columnNumber || 0,
561+
};
562+
} else {
563+
return {
564+
message: `Uncaught ${String(maybeError)}`,
565+
filename: '',
566+
lineno: 0,
567+
colno: 0,
568+
};
569+
}
570+
};
571+
572+
const {message, filename, lineno, colno} = getErrorInfo(errorArg);
558573

559574
let event: ErrorEvent | undefined = undefined;
560575
if (ErrorEvent.prototype.initErrorEvent === undefined) {
@@ -564,7 +579,7 @@ export default class CustomElementInternals {
564579
filename,
565580
lineno,
566581
colno,
567-
error,
582+
error: errorArg,
568583
});
569584
} else {
570585
event = document.createEvent('ErrorEvent') as ErrorEvent;
@@ -587,7 +602,7 @@ export default class CustomElementInternals {
587602
configurable: true,
588603
enumerable: true,
589604
get: function () {
590-
return error;
605+
return errorArg;
591606
},
592607
});
593608
}
@@ -598,7 +613,7 @@ export default class CustomElementInternals {
598613
// console if their associated ErrorEvent isn't handled during dispatch
599614
// (indicated by calling `preventDefault`). In practice, these errors are
600615
// always displayed.
601-
console.error(error);
616+
console.error(errorArg);
602617
}
603618
}
604619
}

packages/custom-elements/ts_src/CustomElementRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export default class CustomElementRegistry {
336336
try {
337337
return this.internal_reifyDefinition(localName, constructorGetter());
338338
} catch (e: unknown) {
339-
this._internals.reportTheException(e as Error);
339+
this._internals.reportTheException(e);
340340
}
341341
}
342342

0 commit comments

Comments
 (0)