From c495be2cf57c0ffebcbc09cc46a90a9a3acb8551 Mon Sep 17 00:00:00 2001 From: Chen-Pang He Date: Thu, 21 Sep 2023 16:01:44 +0800 Subject: [PATCH] Handle external errors with anyhow::Error Reserve panics for internal programming errors only for better UX --- src/fun/mod.rs | 6 +++--- src/weeb.rs | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/fun/mod.rs b/src/fun/mod.rs index ef938c1..cdd4fbc 100644 --- a/src/fun/mod.rs +++ b/src/fun/mod.rs @@ -14,9 +14,9 @@ async fn face_image(user: &serenity::User) -> anyhow::Result, 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::().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(()) } @@ -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(()) } @@ -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(()) } @@ -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::().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(()) } \ No newline at end of file