Skip to content

Commit db9d54a

Browse files
authored
fix(icon): throw error once per session if sprite icon fails to load (#1285)
* fix(icon): handle undefined sprite loading with console warning * fix(icon): ensure sprite loading throws an error on failure * fix(icon): refactor ErrorThrower to accept dynamic error messages
1 parent 745dad9 commit db9d54a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class LitElement extends BasicElement {
4747
*/
4848
export { WarningNotice } from './notices/WarningNotice.js';
4949
export { DeprecationNotice } from './notices/DeprecationNotice.js';
50+
export { ErrorThrower } from './notices/ErrorThrower.js';
5051

5152
/**
5253
* Export events
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export class ErrorThrower {
2+
/**
3+
* Static list to track if any error message has been shown
4+
* across the entire application session
5+
*/
6+
private static readonly shownErrors = new Set<string>();
7+
8+
/**
9+
* Checks if this error has been shown already
10+
* @param {string} message The error message to check
11+
* @returns {boolean} True if the error has been shown
12+
*/
13+
public isShown(message: string): boolean {
14+
return ErrorThrower.shownErrors.has(message);
15+
}
16+
17+
/**
18+
* Shows the error notice and marks it as shown
19+
* @param {string} message The error message to throw
20+
* @returns {void}
21+
*/
22+
public throw(message: string): void {
23+
ErrorThrower.shownErrors.add(message);
24+
throw new Error(message);
25+
}
26+
27+
/**
28+
* Shows the error notice only once per application session
29+
* @param {string} message The error message to throw
30+
* @returns {void}
31+
*/
32+
public once(message: string): void {
33+
if (!this.isShown(message)) {
34+
this.throw(message);
35+
}
36+
}
37+
}

packages/elements/src/icon/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BasicElement,
55
CSSResultGroup,
66
DeprecationNotice,
7+
ErrorThrower,
78
PropertyValues,
89
SVGTemplateResult,
910
TemplateResult,
@@ -37,6 +38,8 @@ const EmptyTemplate = svg``;
3738
*/
3839
export const iconTemplateCache = new Map<string, Promise<SVGTemplateResult>>();
3940

41+
const errorThrower = new ErrorThrower();
42+
4043
@customElement('ef-icon')
4144
export class Icon extends BasicElement {
4245
/**
@@ -230,7 +233,9 @@ export class Icon extends BasicElement {
230233
} else if (isUrl(iconProperty) || IconLoader.isPrefixResolved) {
231234
void this.loadAndRenderIcon(iconProperty);
232235
} else {
233-
void this.loadAndRenderSpriteIcon(iconProperty);
236+
void this.loadAndRenderSpriteIcon(iconProperty).catch((error) => {
237+
errorThrower.once(String(error));
238+
});
234239
}
235240
}
236241

0 commit comments

Comments
 (0)