Skip to content

Commit 4b03026

Browse files
authored
Improve error handling for missing icons (FortAwesome#13)
1 parent 42b5de7 commit 4b03026

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

examples/example.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ <h3>Animation</h3>
2222
<p>Slide to turn up the magic.</p>
2323
<fa-icon [icon]="faMagic" transform="rotate-{{magicLevel}}"></fa-icon>
2424
<input type='range' [value]="magicLevel" (input)="magicLevel=$event.target.value"/>
25+
<h3>Intentionally Missing Icon</h3>
26+
<p>Expect to see an error on the JavaScript console, and nothing shown between these bars: |<fa-icon [icon]="faFluzzle"></fa-icon>|</p>
27+

src/icon.component.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
RotateProp,
1212
SizeProp,
1313
Transform,
14+
config
1415
} from '@fortawesome/fontawesome';
1516

1617
function isIconLookup(i: IconProp): i is IconLookup {
@@ -126,7 +127,19 @@ export class FaIconComponent implements OnChanges {
126127
symbol: this.symbol
127128
});
128129

129-
// @TODO: make sure that it doesn't break things to do html[0] here.
130-
this.renderedIconHTML = this.sanitizer.bypassSecurityTrustHtml(renderedIcon.html[0]);
130+
let html;
131+
if(renderedIcon){
132+
html = renderedIcon.html[0];
133+
} else {
134+
html = `<svg class="${config.replacementClass}" viewBox="0 0 448 512"></svg><!--icon not found-->`;
135+
if(iconSpec == null) {
136+
console.error('Could not find icon. ' +
137+
`It looks like you've provided a null or undefined icon object to this component.`);
138+
} else {
139+
console.error(`Could not find icon: iconName=${iconSpec.iconName}, prefix=${iconSpec.prefix}`);
140+
}
141+
}
142+
143+
this.renderedIconHTML = this.sanitizer.bypassSecurityTrustHtml(html);
131144
}
132145
}

0 commit comments

Comments
 (0)