Fix error handling
This commit is contained in:
parent
d96fcf79dd
commit
237293badd
15
src/lib.rs
15
src/lib.rs
@ -62,9 +62,14 @@ pub fn guild_by_prefix(prefix: &str) -> Result<Option<GuildEntry>, 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)]
|
||||
@ -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<String>
|
||||
|
Loading…
Reference in New Issue
Block a user