From 3e3be2aed507360018933de1e47354910169ef9a Mon Sep 17 00:00:00 2001 From: seth Date: Wed, 13 Dec 2023 11:47:08 -0500 Subject: [PATCH] feat: better handle errors during setup --- src/handlers/error.rs | 9 ++++++++- src/main.rs | 30 +++++++++++++++++++++++------- src/storage/mod.rs | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/handlers/error.rs b/src/handlers/error.rs index de2121e..66277e0 100644 --- a/src/handlers/error.rs +++ b/src/handlers/error.rs @@ -8,7 +8,14 @@ use poise::FrameworkError; pub async fn handle(error: FrameworkError<'_, Data, Report>) { match error { - FrameworkError::Setup { error, .. } => error!("Error setting up client!\n{error:#?}"), + FrameworkError::Setup { + error, framework, .. + } => { + error!("Error setting up client! Bailing out"); + framework.shard_manager().lock().await.shutdown_all().await; + + panic!("{error}") + } FrameworkError::Command { error, ctx } => { error!("Error in command {}:\n{error:?}", ctx.command().name); diff --git a/src/main.rs b/src/main.rs index 92544fa..f3b47e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,19 @@ use std::sync::Arc; use std::time::Duration; -use color_eyre::eyre::{Context as _, Report, Result}; +use color_eyre::eyre::{eyre, Context as _, Report, Result}; use color_eyre::owo_colors::OwoColorize; -use config::Config; + use log::*; + use poise::{ - serenity_prelude::{self as serenity, ShardManager}, - EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions, + serenity_prelude as serenity, EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions, }; -use storage::Storage; + +use serenity::ShardManager; + +use redis::ConnectionLike; + use tokio::signal::ctrl_c; use tokio::signal::unix::{signal, SignalKind}; use tokio::sync::Mutex; @@ -23,6 +27,9 @@ mod storage; mod tags; mod utils; +use config::Config; +use storage::Storage; + type Context<'a> = poise::Context<'a, Data, Report>; #[derive(Clone)] @@ -51,11 +58,20 @@ async fn setup( _ready: &serenity::Ready, framework: &Framework, ) -> Result { + let data = Data::new()?; + + // test redis connection + let mut client = data.storage.client.clone(); + + if !client.check_connection() { + return Err(eyre!( + "Couldn't connect to storage! Is your daemon running?" + )); + } + poise::builtins::register_globally(ctx, &framework.options().commands).await?; info!("Registered global commands!"); - let data = Data::new()?; - Ok(data) } diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 3f0d419..885817a 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -13,7 +13,7 @@ const LAUNCHER_VERSION_KEY: &str = "launcher-version-v1"; #[derive(Clone, Debug)] pub struct Storage { - client: Client, + pub client: Client, } impl Storage {