feat: better handle errors during setup

This commit is contained in:
seth 2023-12-13 11:47:08 -05:00
parent 9ae8b98e1a
commit 3e3be2aed5
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
3 changed files with 32 additions and 9 deletions

View file

@ -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);

View file

@ -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<Data, Report>,
) -> Result<Data> {
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)
}

View file

@ -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 {