Skip to content

Commit eac7fd2

Browse files
programmistkennethcachia
authored andcommitted
fix(icon): Log network errors without throwing exceptions.
fixes angular#2530 closes angular#3718
1 parent c705330 commit eac7fd2

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/components/icon/iconService.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,8 @@
394394
if ( urlRegex.test(id) ) return loadByURL(id).then( cacheIcon(id) );
395395
if ( id.indexOf(':') == -1 ) id = '$default:' + id;
396396

397-
return loadByID(id)
398-
.catch(loadFromIconSet)
399-
.catch(announceIdNotFound)
400-
.catch(announceNotFound)
397+
var load = config[id] ? loadByID : loadFromIconSet;
398+
return load(id)
401399
.then( cacheIcon(id) );
402400
}
403401

@@ -435,9 +433,8 @@
435433
*
436434
*/
437435
function loadByID(id) {
438-
var iconConfig = config[id];
439-
440-
return !iconConfig ? $q.reject(id) : loadByURL(iconConfig.url).then(function(icon) {
436+
var iconConfig = config[id];
437+
return loadByURL(iconConfig.url).then(function(icon) {
441438
return new Icon(icon, iconConfig);
442439
});
443440
}
@@ -450,12 +447,19 @@
450447
var setName = id.substring(0, id.lastIndexOf(':')) || '$default';
451448
var iconSetConfig = config[setName];
452449

453-
return !iconSetConfig ? $q.reject(id) : loadByURL(iconSetConfig.url).then(extractFromSet);
450+
return !iconSetConfig ? announceIdNotFound(id) : loadByURL(iconSetConfig.url).then(extractFromSet);
454451

455452
function extractFromSet(set) {
456453
var iconName = id.slice(id.lastIndexOf(':') + 1);
457454
var icon = set.querySelector('#' + iconName);
458-
return !icon ? $q.reject(id) : new Icon(icon, iconSetConfig);
455+
return !icon ? announceIdNotFound(id) : new Icon(icon, iconSetConfig);
456+
}
457+
458+
function announceIdNotFound(id) {
459+
var msg = 'icon ' + id + ' not found';
460+
$log.warn(msg);
461+
462+
return $q.reject(msg || id);
459463
}
460464
}
461465

@@ -468,22 +472,7 @@
468472
.get(url, { cache: $templateCache })
469473
.then(function(response) {
470474
return angular.element('<div>').append(response.data).find('svg')[0];
471-
});
472-
}
473-
474-
/**
475-
* User did not specify a URL and the ID has not been registered with the $mdIcon
476-
* registry
477-
*/
478-
function announceIdNotFound(id) {
479-
var msg;
480-
481-
if (angular.isString(id)) {
482-
msg = 'icon ' + id + ' not found';
483-
$log.warn(msg);
484-
}
485-
486-
return $q.reject(msg || id);
475+
}).catch(announceNotFound);
487476
}
488477

489478
/**

src/components/icon/iconService.spec.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('mdIcon service', function() {
1111
$mdIconProvider
1212
.icon('android' , 'android.svg')
1313
.icon('c2' , 'c2.svg')
14+
.icon('notfound' ,'notfoundicon.svg')
1415
.iconSet('social' , 'social.svg' )
1516
.iconSet('notfound' , 'notfoundgroup.svg' )
1617
.defaultIconSet('core.svg');
@@ -26,6 +27,7 @@ describe('mdIcon service', function() {
2627
$templateCache.put('c2.svg' , '<svg><g id="c2" class="override"></g></svg>');
2728

2829
$httpBackend.whenGET('notfoundgroup.svg').respond(404, 'Cannot GET notfoundgroup.svg');
30+
$httpBackend.whenGET('notfoundicon.svg').respond(404, 'Cannot GET notfoundicon.svg');
2931

3032
}));
3133

@@ -104,7 +106,7 @@ describe('mdIcon service', function() {
104106
});
105107

106108
describe('icon set URL is not found', function() {
107-
it('should throw Error', function() {
109+
it('should log Error', function() {
108110
var msg;
109111
try {
110112
$mdIcon('notconfigured')
@@ -119,8 +121,8 @@ describe('mdIcon service', function() {
119121
});
120122
});
121123

122-
describe('icon is not found', function() {
123-
it('should throw Error', function() {
124+
describe('icon group is not found', function() {
125+
it('should log Error', function() {
124126
var msg;
125127
try {
126128
$mdIcon('notfound:someIcon')
@@ -135,6 +137,15 @@ describe('mdIcon service', function() {
135137
});
136138
});
137139

140+
describe('icon is not found', function() {
141+
it('should not throw Error', function() {
142+
expect(function(){
143+
$mdIcon('notfound');
144+
145+
$httpBackend.flush();
146+
}).not.toThrow();
147+
});
148+
});
138149
});
139150

140151
function updateDefaults(svg) {

0 commit comments

Comments
 (0)