Skip to content

Commit

Permalink
Add some error handling for grabbing per OSM-way data; sometimes it c…
Browse files Browse the repository at this point in the history
…rashes.

Still not sure why, but the next error message should be more useful
  • Loading branch information
dabreegster committed Mar 24, 2024
1 parent 53b83f1 commit e2abf57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 12 additions & 5 deletions osm2streets-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ impl JsStreetNetwork {
// TODO I think https://github.com/cloudflare/serde-wasm-bindgen would let us just return a
// HashMap
#[wasm_bindgen(js_name = getOsmTagsForWay)]
pub fn get_osm_tags_for_way(&self, id: i64) -> String {
serde_json::to_string_pretty(&self.ways[&osm::WayID(id)].tags).unwrap()
pub fn get_osm_tags_for_way(&self, id: i64) -> Result<String, JsValue> {
if let Some(ref way) = self.ways.get(&osm::WayID(id)) {
Ok(serde_json::to_string_pretty(&way.tags).unwrap())
} else {
Err(JsValue::from_str(&format!("unknown way {id}")))
}
}

/// Returns the entire StreetNetwork as JSON. The API doesn't have guarantees about backwards
Expand Down Expand Up @@ -231,8 +235,11 @@ impl JsStreetNetwork {
/// Returns the XML string representing a way. Any OSM tags changed via
/// `overwrite_osm_tags_for_way` are reflected.
#[wasm_bindgen(js_name = wayToXml)]
pub fn way_to_xml(&self, id: i64) -> String {
let way = &self.ways[&osm::WayID(id)];
pub fn way_to_xml(&self, id: i64) -> Result<String, JsValue> {
let Some(ref way) = self.ways.get(&osm::WayID(id)) else {
return Err(JsValue::from_str(&format!("unknown way {id}")));
};

let mut out = format!(r#"<way id="{id}""#);
if let Some(version) = way.version {
out.push_str(&format!(r#" version="{version}""#));
Expand All @@ -247,7 +254,7 @@ impl JsStreetNetwork {
out.push('\n');
}
out.push_str("</way>");
out
Ok(out)
}
}

Expand Down
9 changes: 7 additions & 2 deletions web/src/lane-editor/AllEdits.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
contents += `<create/>\n`;
contents += `<modify>\n`;
for (let id of editedWays) {
contents += $network!.wayToXml(id);
contents += "\n";
try {
contents += $network!.wayToXml(id);
contents += "\n";
} catch (err) {
// TODO Not sure why this happens, but just skip this edit
console.error(err);
}
}
contents += `</modify>\n`;
contents += `</osmChange>`;
Expand Down

0 comments on commit e2abf57

Please sign in to comment.