From 1c544f456a566ca591ef49f2d775f875641007dd Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 15 Nov 2017 09:28:31 +0800 Subject: [PATCH 1/4] check if before layer exists --- src/style/style.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/style/style.js b/src/style/style.js index 6ee38275f6e..a5ad899d224 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -622,6 +622,10 @@ class Style extends Evented { this._order.splice(index, 1); const newIndex = before ? this._order.indexOf(before) : this._order.length; + if (before && newIndex === -1) { + this.fire('error', { message: new Error(`Layer with id "${before}" does not exist on this map.`)}); + return; + } this._order.splice(newIndex, 0, id); this._layerOrderChanged = true; From 035d5adb1aee968c1b1b9a10d9fd75deed5bffc8 Mon Sep 17 00:00:00 2001 From: jingsam Date: Thu, 16 Nov 2017 21:15:24 +0800 Subject: [PATCH 2/4] fix JSDoc --- src/style/style.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/style/style.js b/src/style/style.js index a5ad899d224..f20c9873ad5 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -543,7 +543,7 @@ class Style extends Evented { /** * Add a layer to the map style. The layer will be inserted before the layer with * ID `before`, or appended if `before` is omitted. - * @param {string=} before ID of an existing layer to insert before + * @param {string} before ID of an existing layer to insert before */ addLayer(layerObject: LayerSpecification, before?: string, options?: {validate?: boolean}) { this._checkLoaded(); @@ -599,9 +599,10 @@ class Style extends Evented { } /** - * Add a layer to the map style. The layer will be inserted before the layer with + * Moves a layer to a different z-position. The layer will be inserted before the layer with * ID `before`, or appended if `before` is omitted. - * @param {string=} before ID of an existing layer to insert before + * @param {string} id ID of the layer to move + * @param {string} before ID of an existing layer to insert before */ moveLayer(id: string, before?: string) { this._checkLoaded(); From 2ae34b16ac3f552227bf36e56f4215c9f4c33913 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 21 Nov 2017 10:38:01 +0800 Subject: [PATCH 3/4] rename message to error --- src/style/style.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/style/style.js b/src/style/style.js index f20c9873ad5..aa972f09bf3 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -568,7 +568,7 @@ class Style extends Evented { const index = before ? this._order.indexOf(before) : this._order.length; if (before && index === -1) { - this.fire('error', { message: new Error(`Layer with id "${before}" does not exist on this map.`)}); + this.fire('error', { error: new Error(`Layer with id "${before}" does not exist on this map.`)}); return; } @@ -624,7 +624,7 @@ class Style extends Evented { const newIndex = before ? this._order.indexOf(before) : this._order.length; if (before && newIndex === -1) { - this.fire('error', { message: new Error(`Layer with id "${before}" does not exist on this map.`)}); + this.fire('error', { error: new Error(`Layer with id "${before}" does not exist on this map.`)}); return; } this._order.splice(newIndex, 0, id); From 8189698d4554400f66023961af338d96348d88e8 Mon Sep 17 00:00:00 2001 From: jingsam Date: Tue, 21 Nov 2017 12:27:30 +0800 Subject: [PATCH 4/4] fix tests --- test/unit/style/style.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 1c9b043e77d..61b095564a0 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -1022,7 +1022,7 @@ test('Style#addLayer', (t) => { style.on('style.load', () => { style.on('error', (error)=>{ - t.match(error.message, /does not exist on this map/); + t.match(error.error, /does not exist on this map/); t.end(); }); style.addLayer(layer, 'z');