From ffd8a15edacf8deded93d4939429b94f0866f766 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 19 Jul 2020 22:02:57 +0100 Subject: [PATCH] give feedback on errors --- src/lib.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c0ce224..d85fb17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,14 +26,25 @@ fn tile_name(prefix: &str, index: &u32) -> Option { /// prefix will be ignored if empty #[wasm_bindgen] pub fn add_tiles(game_data: String, image: String, prefix: String) -> String { - let mut game = Game::from(game_data) - .expect("Couldn't parse game data"); + let game = Game::from(game_data); + if game.is_err() { + return format!("Couldn't parse game data"); + } + let mut game = game.unwrap(); let image: Vec<&str> = image.split("base64,").collect(); let image = image[1]; - let image = base64::decode(image).unwrap(); - let image = image::load_from_memory(image.as_ref()) - .expect("Couldn't load image"); + + let image = base64::decode(image); + if image.is_err() { + return format!("Couldn't decode image"); + } + let image = image.unwrap(); + let image = image::load_from_memory(image.as_ref()); + if image.is_err() { + return format!("Couldn't load image"); + } + let image = image.unwrap(); let width = image.width(); let height = image.height();