Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent calls to getLayoutProperty from causing fatal error when layer doesn't exist #7539

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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