Skip to content

Commit

Permalink
check if before layer exists (#5679)
Browse files Browse the repository at this point in the history
* check if before layer exists

* fix JSDoc

* rename message to error

* fix tests
  • Loading branch information
jingsam authored and mollymerp committed Nov 30, 2017
1 parent 601a9bd commit 5fa04bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,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();
Expand All @@ -572,7 +572,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;
}

Expand Down Expand Up @@ -602,9 +602,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();
Expand All @@ -625,6 +626,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', { error: new Error(`Layer with id "${before}" does not exist on this map.`)});
return;
}
this._order.splice(newIndex, 0, id);

this._layerOrderChanged = true;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,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');
Expand Down

0 comments on commit 5fa04bc

Please sign in to comment.