From e9e683d0030d9a459ecd6183c8250d0dc42662ef Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sun, 3 Mar 2019 12:22:22 -0500 Subject: improve error handling --- src/commands/meme.rs | 48 +++++++++++++++++++++++++++++++++++------------- src/main.rs | 6 +++++- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/commands/meme.rs b/src/commands/meme.rs index 42a4051..8cdb062 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -6,7 +6,11 @@ use std::{ }, }; -use diesel::PgConnection; +use diesel::{ + NotFound, + PgConnection, + result::Error as DieselError, +}; use failure::Error; use rand::{Rng, thread_rng}; use serenity::{ @@ -22,14 +26,13 @@ use timeago::{ }; use url::Url; -use audio::ytdl_url; - use crate::{ audio::{ CtxExt, parse_times, PlayArgs, PlayQueue, + ytdl_url, }, commands::send, db::{ @@ -75,15 +78,12 @@ fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_only: bool) -> Resu x }, Err(e) => { - use diesel::{NotFound, self}; - - if let Some(NotFound) = e.downcast_ref::() { - send(msg.channel_id, "c'mon baby, guesstimate", msg.tts)?; + return if let Some(NotFound) = e.downcast_ref::() { + send(msg.channel_id, "c'mon baby, guesstimate", msg.tts) } else { send(msg.channel_id, "what in ryan's name", msg.tts)?; - } - - return Err(e) + Err(e) + }; }, }; @@ -95,8 +95,16 @@ pub fn wat(_: &mut Context, msg: &Message, _: Args) -> Result<()> { let record = match InvocationRecord::last(&conn) { Ok(x) => x, - Err(_) => return send(msg.channel_id, "no one has ever memed before", msg.tts), + Err(e) => { + if let Some(NotFound) = e.downcast_ref::() { + return send(msg.channel_id, "no one has ever memed before", msg.tts); + } + + send(msg.channel_id, "BAD MEME BAD MEME", msg.tts)?; + return Err(e); + }, }; + let meme = Meme::find(&conn, record.meme_id); match meme { @@ -108,7 +116,14 @@ pub fn wat(_: &mut Context, msg: &Message, _: Args) -> Result<()> { &format!("that was \"{}\" by {} ({})", meme.title, author.mention(), metadata.created.date()), msg.tts)? }, - Err(_) => send(msg.channel_id, "heuueueeeeh?", msg.tts)?, + Err(e) => { + if let Some(NotFound) = e.downcast_ref::() { + return send(msg.channel_id, "heuueueeeeh?", msg.tts); + } + + send(msg.channel_id, "do i look like i know what a jpeg is", msg.tts)?; + return Err(e); + }, }; meme.map(|_| {}) @@ -156,7 +171,13 @@ pub fn history(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> { let invoker_name = crate::TARGET_GUILD_ID.member(rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); format!("{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", i + 1, rand, ago, meme.title, author_name, metadata.created.date(), invoker_name) }) - .unwrap_or_else(|_| { + .unwrap_or_else(|e| { + if let Some(variant) = e.downcast_ref::() { + if *variant != NotFound { + error!("error encountered loading meme history: {}", e); + } + } + let invoker_name = crate::TARGET_GUILD_ID.member(rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) }) @@ -208,6 +229,7 @@ pub fn addaudiomeme(_: &mut Context, msg: &Message, args: Args) -> Result<()> { let elems = audio_str.split_whitespace().collect::>(); if elems.len() == 0 { + send(msg.channel_id, "are you stupid", msg.tts)?; return Err(::failure::err_msg("no audio link was provided")) } diff --git a/src/main.rs b/src/main.rs index 044e6cf..1116b1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -107,12 +107,16 @@ fn run() -> Result<()> { result }) - .after(|_ctx, _msg, cmd, err| { + .after(|_ctx, msg, cmd, err| { match err { Ok(()) => { trace!("command '{}' completed successfully", cmd); }, Err(e) => { + if let Err(e) = crate::commands::send(msg.channel_id, "BANIC", msg.tts) { + error!("sending BANIC: {}", e); + } + error!("error encountered handling command '{}': {:?}", cmd, e); } } -- cgit v1.3.1