Skip to content

Commit

Permalink
fire an error on checking if non-existant source is loaded mapbox#3691
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Jan 23, 2017
1 parent f9f1a82 commit 3ac76b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ class Map extends Camera {
isSourceLoaded(id) {
const source = this.style && this.style.sourceCaches[id];
if (source === undefined) {
throw new Error(`There is no source with ID '${id}'`);
this.fire('error', {
error: new Error(`There is no source with ID '${id}'`)
});
return;
}
return source.loaded();
}
Expand Down
9 changes: 5 additions & 4 deletions test/js/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,11 @@ test('Map', (t) => {
const map = createMap({style: style});

map.on('load', () => {
t.throws(() => {
map.isSourceLoaded('geojson');
}, Error, /There is no source with ID/i);
t.end();
map.on('error', ({ error }) => {
t.match(error.message, /There is no source with ID/);
t.end();
});
map.isSourceLoaded('geojson');
});
});

Expand Down

1 comment on commit 3ac76b0

@localdevjs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the heck do you throw an Error if the source is non existant? Why wouldn't you just return false??? That method can be called for different reasons, one of them being not every source is loaded all the time, so the sane thing to do would be just to return false. How else are you supposed to find out if a source you sometimes add, but not always, is really loaded?

Please sign in to comment.