From 237293badd766f7886619392b5dd46b960afb4b6 Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Thu, 15 Feb 2018 13:37:21 +0100 Subject: [PATCH] Fix error handling --- src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e0c0ca3..2873e74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,9 +62,14 @@ pub fn guild_by_prefix(prefix: &str) -> Result, Error> { } pub fn guild_leaderboard() -> Result { - let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?; + let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?.text()?; - Ok(serde_json::from_reader(resp)?) + if let Ok(error) = serde_json::from_str::(&resp) { + bail!("API error ({})", error.message); + } else { + let list: Top100Guilds = serde_json::from_str(&resp)?; + Ok(list) + } } #[derive(Deserialize)] @@ -72,6 +77,12 @@ pub struct APIError { pub error: String } +#[derive(Deserialize)] +pub struct APIError2 { + pub code: u64, + pub message: String +} + #[derive(Deserialize)] pub struct GuildList { pub guilds: Vec