diff --git a/src/style/style.js b/src/style/style.js index 3f21a686309..ec55b10b8a5 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -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) { diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 5dedb4d5337..2e91fb202b9 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -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, {