diff --git a/src/style/style.js b/src/style/style.js index 4a5201a1682..c6ec6c1ddda 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -569,9 +569,11 @@ class Style extends Evented { layer.setEventedParent(this, {layer: {id: id}}); - const beforeIndex = before ? this._order.indexOf(before) : -1; - const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length; - if (before && beforeIndex === -1) util.warnOnce(`Layer with id "${before}" does not exist on this map. Adding layer to the top of the stack.`); + 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.`)}); + return; + } this._order.splice(index, 0, id); diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index a25a7afaff0..2142dfaf365 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -1006,7 +1006,7 @@ test('Style#addLayer', (t) => { }); }); - t.test('warns if before layer does not exist', (t) => { + t.test('fire error if before layer does not exist', (t) => { const style = new Style(new StubMap()); style.loadJSON(createStyleJSON({ layers: [{ @@ -1018,13 +1018,13 @@ test('Style#addLayer', (t) => { }] })); const layer = {id: 'c', type: 'background'}; - t.stub(util, 'warnOnce'); style.on('style.load', () => { + style.on('error', (error)=>{ + t.match(error.message, /does not exist on this map/); + t.end(); + }); style.addLayer(layer, 'z'); - t.deepEqual(style._order, ['a', 'b', 'c']); - t.ok(util.warnOnce.calledOnce); - t.end(); }); });