From d96fcf79dd6362cdcc7ecb15a7e8fb2588257c3a Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Wed, 14 Feb 2018 18:07:06 +0100 Subject: [PATCH] Improve error handling --- src/lib.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cf65cf5..e0c0ca3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,9 @@ use failure::Error; use std::collections::HashMap; pub fn player(name: &str) -> Result { - let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", name))?.text().unwrap(); + let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", name))? + .error_for_status()? + .text().unwrap(); if let Ok(error) = serde_json::from_str::(&resp) { bail!("API error ({})", error.error); @@ -23,7 +25,9 @@ pub fn player(name: &str) -> Result { } pub fn guild(name: &str) -> Result, Error> { - let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))?.text().unwrap(); + let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))? + .error_for_status()? + .text().unwrap(); if let Ok(error) = serde_json::from_str::(&resp) { bail!("API error ({})", error.error); @@ -33,7 +37,9 @@ pub fn guild(name: &str) -> Result, Error> { } pub fn guild_list() -> Result, Error> { - let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=guildList")?.text().unwrap(); + let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=guildList")? + .error_for_status()? + .text().unwrap(); if let Ok(error) = serde_json::from_str::(&resp) { bail!("API error ({})", error.error); @@ -56,7 +62,7 @@ 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")?; + let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } @@ -105,10 +111,10 @@ pub struct Guild { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] pub struct Member { - pub name: Option, + pub name: String, pub rank: Option, pub contributed: u64, - pub joined: Option, + pub joined: String, pub joined_friendly: String, }