Skip to content

Commit 962092b

Browse files
committed
#79 Notify user if selected object is not a building or if other errors occur
1 parent 6e46da8 commit 962092b

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

src/building.js

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ class Building {
3333
* Create new building
3434
*/
3535
static async create(type, id) {
36-
var data;
37-
if (type === 'way') {
38-
data = await Building.getWayData(id);
39-
} else {
40-
data = await Building.getRelationData(id);
36+
let data;
37+
try {
38+
if (type === 'way') {
39+
data = await Building.getWayData(id);
40+
} else {
41+
data = await Building.getRelationData(id);
42+
}
43+
} catch (e) {
44+
alert(e);
45+
throw e;
4146
}
4247
let xmlData = new window.DOMParser().parseFromString(data, 'text/xml');
4348
const nodelist = Building.buildNodeList(xmlData);
@@ -63,29 +68,32 @@ class Building {
6368
} else {
6469
this.type = 'relation';
6570
}
66-
if (this.isValidData(outerElementXml)) {
67-
this.nodelist = Building.buildNodeList(this.fullXmlData);
68-
this.setHome();
69-
this.repositionNodes();
70-
if (this.type === 'way') {
71+
try {
72+
this.validateData(outerElementXml);
73+
} catch (e) {
74+
const errorText = `Rendering of ${outerElementXml.tagName.toLowerCase()} ${id} is not is not possible. ${e}`;
75+
alert(errorText);
76+
window.printError(errorText);
77+
throw new Error('invalid XML data');
78+
}
79+
this.nodelist = Building.buildNodeList(this.fullXmlData);
80+
this.setHome();
81+
this.repositionNodes();
82+
if (this.type === 'way') {
83+
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
84+
} else if (this.type === 'multipolygon') {
85+
this.outerElement = new MultiBuildingPart(id, this.fullXmlData, this.nodelist);
86+
} else {
87+
const outlineRef = outerElementXml.querySelector('member[role="outline"]').getAttribute('ref');
88+
const outline = this.fullXmlData.getElementById(outlineRef);
89+
const outlineType = outline.tagName.toLowerCase();
90+
if (outlineType === 'way') {
7191
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
72-
} else if (this.type === 'multipolygon') {
73-
this.outerElement = new MultiBuildingPart(id, this.fullXmlData, this.nodelist);
7492
} else {
75-
const outlineRef = outerElementXml.querySelector('member[role="outline"]').getAttribute('ref');
76-
const outline = this.fullXmlData.getElementById(outlineRef);
77-
const outlineType = outline.tagName.toLowerCase();
78-
if (outlineType === 'way') {
79-
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
80-
} else {
81-
this.outerElement = new MultiBuildingPart(outlineRef, this.fullXmlData, this.nodelist);
82-
}
93+
this.outerElement = new MultiBuildingPart(outlineRef, this.fullXmlData, this.nodelist);
8394
}
84-
this.addParts();
85-
} else {
86-
window.printError('XML Not Valid');
87-
throw new Error('invalid XML');
8895
}
96+
this.addParts();
8997
}
9098

9199
/**
@@ -189,15 +197,27 @@ class Building {
189197
static async getWayData(id) {
190198
let restPath = apis.getWay.url(id);
191199
let response = await fetch(restPath);
192-
let text = await response.text();
193-
return text;
200+
if (response.status === 404) {
201+
throw `The way ${id} was not found on the server.\nURL: ${restPath}`;
202+
} else if (response.status === 410) {
203+
throw `The way ${id} was deleted.\nURL: ${restPath}`;
204+
} else if (response.status !== 200) {
205+
throw `HTTP ${response.status}.\nURL: ${restPath}`;
206+
}
207+
return await response.text();
194208
}
195209

196210
static async getRelationData(id) {
197211
let restPath = apis.getRelation.url(id);
198212
let response = await fetch(restPath);
199-
let text = await response.text();
200-
return text;
213+
if (response.status === 404) {
214+
throw `The relation ${id} was not found on the server.\nURL: ${restPath}`;
215+
} else if (response.status === 410) {
216+
throw `The relation ${id} was deleted.\nURL: ${restPath}`;
217+
} else if (response.status !== 200) {
218+
throw `HTTP ${response.status}.\nURL: ${restPath}`;
219+
}
220+
return await response.text();
201221
}
202222

203223
/**
@@ -212,15 +232,15 @@ class Building {
212232
/**
213233
* validate that we have the ID of a building way.
214234
*/
215-
isValidData(xmlData) {
235+
validateData(xmlData) {
216236
// Check that it is a building (<tag k="building" v="*"/> exists)
217237
const buildingType = xmlData.querySelector('[k="building"]');
218238
const ways = [];
219239
if (xmlData.tagName === 'relation') {
220240
// get all building relation parts
221241
// todo: multipolygon inner and outer roles.
222242
let parts = xmlData.querySelectorAll('member[role="part"]');
223-
var ref = 0;
243+
let ref = 0;
224244
for (let i = 0; i < parts.length; i++) {
225245
ref = parts[i].getAttribute('ref');
226246
const part = this.fullXmlData.getElementById(ref);
@@ -232,8 +252,9 @@ class Building {
232252
}
233253
} else {
234254
if (!buildingType) {
235-
window.printError('Outer way is not a building');
236-
return false;
255+
const errorText = 'Outer way is not a building';
256+
window.printError(errorText);
257+
throw errorText;
237258
}
238259
ways.push(xmlData);
239260
}
@@ -246,16 +267,18 @@ class Building {
246267
const firstRef = nodes[0].getAttribute('ref');
247268
const lastRef = nodes[nodes.length - 1].getAttribute('ref');
248269
if (firstRef !== lastRef) {
249-
window.printError('Way ' + way.getAttribute('id') + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.');
250-
return false;
270+
const errorText = 'Way ' + way.getAttribute('id') + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.';
271+
window.printError(errorText);
272+
throw errorText;
251273
}
252274
} else {
253-
window.printError('Way ' + way.getAttribute('id') + ' has no nodes.');
254-
return false;
275+
const errorText = 'Way ' + way.getAttribute('id') + ' has no nodes.';
276+
window.printError(errorText);
277+
throw errorText;
255278
}
256279
} else {
257280
let parts = way.querySelectorAll('member[role="part"]');
258-
var ref = 0;
281+
let ref = 0;
259282
for (let i = 0; i < parts.length; i++) {
260283
ref = parts[i].getAttribute('ref');
261284
const part = this.fullXmlData.getElementById(ref);

0 commit comments

Comments
 (0)