Improve error handling

This commit is contained in:
Sakuhl 2018-01-28 15:38:14 +01:00
parent 4ac71c47d3
commit 1a84202d2b
3 changed files with 7 additions and 4 deletions

2
Cargo.lock generated
View File

@ -1125,7 +1125,7 @@ dependencies = [
[[package]]
name = "wynnrobot"
version = "0.10.0"
version = "0.10.1"
dependencies = [
"diesel 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -35,7 +35,8 @@ pub fn wc_guild(command: &str, msg: &Message) -> Result<(), Error> {
}
}
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList")?.text().unwrap();
let message = (|| {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList")?.text()?;
let territories: Value = if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
bail!("API error ({})", error.error);
@ -54,6 +55,8 @@ pub fn wc_guild(command: &str, msg: &Message) -> Result<(), Error> {
//territories_count += 1;
}
}
Ok(message)
})().unwrap_or_else(|e| format!("\nError getting territories: {}", e));
let full_msg = format!(
"**Guild**: {}

View File

@ -111,7 +111,7 @@ Visit the Discord for more help: https://discord.gg/GbN7HeG"))?;
pub fn wc_crop(command: &str, msg: &Message) -> Result<(), Error> {
let url = &command[5..];
let new_url = reqwest::get(&format!("https://wynncraft-autocrop.herokuapp.com/crop?url={}", url))?.text().unwrap();
if new_url.starts_with("https://i.imgur.com") {
if new_url.starts_with("https://res.cloudinary.com") {
msg.channel_id.say(&new_url)?;
} else {
bail!(failure::err_msg("could not crop image"));
@ -125,7 +125,7 @@ pub fn wc_crop_discord_upload(msg: &Message) -> Result<(), Error> {
for attachment in &last.attachments {
let url = &attachment.url;
let new_url = reqwest::get(&format!("https://wynncraft-autocrop.herokuapp.com/crop?url={}", url))?.text().unwrap();
if new_url.starts_with("https://i.imgur.com") {
if new_url.starts_with("https://res.cloudinary.com") {
msg.channel_id.say(&new_url)?;
} else {
bail!(failure::err_msg("could not crop image"));