Fix error handling

This commit is contained in:
Sakuhl 2018-02-15 13:37:21 +01:00
parent d96fcf79dd
commit 237293badd

View File

@ -62,9 +62,14 @@ pub fn guild_by_prefix(prefix: &str) -> Result<Option<GuildEntry>, Error> {
} }
pub fn guild_leaderboard() -> Result<Top100Guilds, Error> { pub fn guild_leaderboard() -> Result<Top100Guilds, Error> {
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::<APIError2>(&resp) {
bail!("API error ({})", error.message);
} else {
let list: Top100Guilds = serde_json::from_str(&resp)?;
Ok(list)
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -72,6 +77,12 @@ pub struct APIError {
pub error: String pub error: String
} }
#[derive(Deserialize)]
pub struct APIError2 {
pub code: u64,
pub message: String
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GuildList { pub struct GuildList {
pub guilds: Vec<String> pub guilds: Vec<String>