Skip to content

Commit

Permalink
Handle external errors with anyhow::Error
Browse files Browse the repository at this point in the history
Reserve panics for internal programming errors only for better UX
  • Loading branch information
jdh8 committed Sep 21, 2023
1 parent 3688f1f commit c495be2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async fn face_image(user: &serenity::User) -> anyhow::Result<image::DynamicImage
let extension = std::path::Path::new(&uri).extension();

if extension.map_or(false, |e| e.eq_ignore_ascii_case("webp")) {
Ok(webp::Decoder::new(&buffer).decode()
.expect("Failed to decode WebP avatar")
.to_image())
webp::Decoder::new(&buffer).decode()
.map(|i| i.to_image())
.ok_or_else(|| anyhow::anyhow!("Failed to decode WebP avatar"))
}
else {
Ok(image::load_from_memory(&buffer)?)
Expand Down
12 changes: 8 additions & 4 deletions src/weeb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ pub async fn feed(ctx: Context<'_>,
let text = text.as_deref().unwrap_or("a random anime character");
let endpoint = "https://nekos.life/api/v2/img/feed";
let json = reqwest::get(endpoint).await?.json::<serde_json::Value>().await?;
let url = json["url"].as_str().ok_or_else(|| anyhow::anyhow!("Invalid image URL"))?;

ctx.send(|m| m.embed(|e| e
.description(ctx.author().to_string() + " fed " + text + "!")
.image(json["url"].as_str().expect("Invalid image URL"))
.image(url)
)).await?;
Ok(())
}
Expand All @@ -38,7 +40,7 @@ pub async fn hug(ctx: Context<'_>,
let text = text.as_deref().unwrap_or("Yuri");
ctx.send(|m| m.embed(|e| e
.description(ctx.author().to_string() + " hugged " + text + "!")
.image(HUGS.choose(&mut rand::thread_rng()).expect("Invalid image URL"))
.image(HUGS.choose(&mut rand::thread_rng()).expect("Choosing from an empty image list!"))
)).await?;
Ok(())
}
Expand Down Expand Up @@ -66,7 +68,7 @@ pub async fn kiss(ctx: Context<'_>,
let text = text.as_deref().unwrap_or("Natsuki");
ctx.send(|m| m.embed(|e| e
.description(ctx.author().to_string() + " kissed " + text + "!")
.image(KISSES.choose(&mut rand::thread_rng()).expect("Invalid image URL"))
.image(KISSES.choose(&mut rand::thread_rng()).expect("Choosing from an empty image list!"))
)).await?;
Ok(())
}
Expand Down Expand Up @@ -109,9 +111,11 @@ pub async fn lick(ctx: Context<'_>,
pub async fn neko(ctx: Context<'_>) -> anyhow::Result<()> {
let endpoint = "https://nekos.life/api/v2/img/neko";
let json = reqwest::get(endpoint).await?.json::<serde_json::Value>().await?;
let url = json["url"].as_str().ok_or_else(|| anyhow::anyhow!("Invalid image URL"))?;

ctx.send(|m| m.embed(|e| e
.description("Here comes your random neko.")
.image(json["url"].as_str().expect("Invalid image URL"))
.image(url)
)).await?;
Ok(())
}

0 comments on commit c495be2

Please sign in to comment.