Skip to content

Commit

Permalink
Prevent calls to getLayoutProperty from causing fatal error when laye…
Browse files Browse the repository at this point in the history
…r doesn't exist (#7539)
  • Loading branch information
ryanhamley authored Nov 2, 2018
1 parent 750e601 commit c8bb20d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,18 @@ class Style extends Evented {

/**
* Get a layout property's value from a given layer
* @param {string} layer the layer to inspect
* @param {string} layerId the layer to inspect
* @param {string} name the name of the layout property
* @returns {*} the property value
*/
getLayoutProperty(layer: string, name: string) {
return this.getLayer(layer).getLayoutProperty(name);
getLayoutProperty(layerId: string, name: string) {
const layer = this.getLayer(layerId);
if (!layer) {
this.fire(new ErrorEvent(new Error(`The layer '${layerId}' does not exist in the map's style.`)));
return;
}

return layer.getLayoutProperty(name);
}

setPaintProperty(layerId: string, name: string, value: any) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,28 @@ test('Map', (t) => {
t.end();
});

t.test('#getLayoutProperty', (t) => {
t.test('fires an error if layer not found', (t) => {
const map = createMap(t, {
style: {
version: 8,
sources: {},
layers: []
}
});

map.on('style.load', () => {
map.on('error', ({ error }) => {
t.match(error.message, /does not exist in the map\'s style/);
t.end();
});
map.getLayoutProperty('non-existant', 'text-transform', 'lowercase');
});
});

t.end();
});

t.test('#setPaintProperty', (t) => {
t.test('sets property', (t) => {
const map = createMap(t, {
Expand Down

0 comments on commit c8bb20d

Please sign in to comment.