Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(icon): Log network errors without throwing exceptions.
Browse files Browse the repository at this point in the history
fixes #2530

closes #3718
  • Loading branch information
programmist authored and Robert Messerle committed Aug 18, 2015
1 parent cc95924 commit e2a8f29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
39 changes: 14 additions & 25 deletions src/components/icon/iconService.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,8 @@
if ( urlRegex.test(id) ) return loadByURL(id).then( cacheIcon(id) );
if ( id.indexOf(':') == -1 ) id = '$default:' + id;

return loadByID(id)
.catch(loadFromIconSet)
.catch(announceIdNotFound)
.catch(announceNotFound)
var load = config[id] ? loadByID : loadFromIconSet;
return load(id)
.then( cacheIcon(id) );
}

Expand Down Expand Up @@ -435,9 +433,8 @@
*
*/
function loadByID(id) {
var iconConfig = config[id];

return !iconConfig ? $q.reject(id) : loadByURL(iconConfig.url).then(function(icon) {
var iconConfig = config[id];
return loadByURL(iconConfig.url).then(function(icon) {
return new Icon(icon, iconConfig);
});
}
Expand All @@ -450,12 +447,19 @@
var setName = id.substring(0, id.lastIndexOf(':')) || '$default';
var iconSetConfig = config[setName];

return !iconSetConfig ? $q.reject(id) : loadByURL(iconSetConfig.url).then(extractFromSet);
return !iconSetConfig ? announceIdNotFound(id) : loadByURL(iconSetConfig.url).then(extractFromSet);

function extractFromSet(set) {
var iconName = id.slice(id.lastIndexOf(':') + 1);
var icon = set.querySelector('#' + iconName);
return !icon ? $q.reject(id) : new Icon(icon, iconSetConfig);
return !icon ? announceIdNotFound(id) : new Icon(icon, iconSetConfig);
}

function announceIdNotFound(id) {
var msg = 'icon ' + id + ' not found';
$log.warn(msg);

return $q.reject(msg || id);
}
}

Expand All @@ -468,22 +472,7 @@
.get(url, { cache: $templateCache })
.then(function(response) {
return angular.element('<div>').append(response.data).find('svg')[0];
});
}

/**
* User did not specify a URL and the ID has not been registered with the $mdIcon
* registry
*/
function announceIdNotFound(id) {
var msg;

if (angular.isString(id)) {
msg = 'icon ' + id + ' not found';
$log.warn(msg);
}

return $q.reject(msg || id);
}).catch(announceNotFound);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/components/icon/iconService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('mdIcon service', function() {
$mdIconProvider
.icon('android' , 'android.svg')
.icon('c2' , 'c2.svg')
.icon('notfound' ,'notfoundicon.svg')
.iconSet('social' , 'social.svg' )
.iconSet('notfound' , 'notfoundgroup.svg' )
.defaultIconSet('core.svg');
Expand All @@ -26,6 +27,7 @@ describe('mdIcon service', function() {
$templateCache.put('c2.svg' , '<svg><g id="c2" class="override"></g></svg>');

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

}));

Expand Down Expand Up @@ -104,7 +106,7 @@ describe('mdIcon service', function() {
});

describe('icon set URL is not found', function() {
it('should throw Error', function() {
it('should log Error', function() {
var msg;
try {
$mdIcon('notconfigured')
Expand All @@ -119,8 +121,8 @@ describe('mdIcon service', function() {
});
});

describe('icon is not found', function() {
it('should throw Error', function() {
describe('icon group is not found', function() {
it('should log Error', function() {
var msg;
try {
$mdIcon('notfound:someIcon')
Expand All @@ -135,6 +137,15 @@ describe('mdIcon service', function() {
});
});

describe('icon is not found', function() {
it('should not throw Error', function() {
expect(function(){
$mdIcon('notfound');

$httpBackend.flush();
}).not.toThrow();
});
});
});

function updateDefaults(svg) {
Expand Down

0 comments on commit e2a8f29

Please sign in to comment.