diff --git a/src/style/style.js b/src/style/style.js index f1f46aa0d39..418185e8611 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -521,6 +521,11 @@ class Style extends Evented { const id = layerObject.id; + if (this.getLayer(id)) { + this.fire('error', {error: new Error(`Layer with id "${id}" already exists on this map`)}); + return; + } + if (typeof layerObject.source === 'object') { this.addSource(id, layerObject.source); layerObject = util.clone(layerObject); diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 997f759e871..ae944952cfb 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -997,15 +997,13 @@ test('Style#addLayer', (t) => { const layer = {id: 'background', type: 'background'}; style.on('error', (e) => { - t.deepEqual(e.layer, {id: 'background'}); - t.notOk(/duplicate/.match(e.error.message)); + t.match(e.error, /already exists/); t.end(); }); style.on('style.load', () => { style.addLayer(layer); style.addLayer(layer); - t.end(); }); });