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>) { pub async fn handle(error: FrameworkError<'_, Data, Report>) {
match error { 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 } => { FrameworkError::Command { error, ctx } => {
error!("Error in command {}:\n{error:?}", ctx.command().name); error!("Error in command {}:\n{error:?}", ctx.command().name);

View file

@ -1,15 +1,19 @@
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; 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 color_eyre::owo_colors::OwoColorize;
use config::Config;
use log::*; use log::*;
use poise::{ use poise::{
serenity_prelude::{self as serenity, ShardManager}, serenity_prelude as serenity, EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions,
EditTracker, Framework, FrameworkOptions, PrefixFrameworkOptions,
}; };
use storage::Storage;
use serenity::ShardManager;
use redis::ConnectionLike;
use tokio::signal::ctrl_c; use tokio::signal::ctrl_c;
use tokio::signal::unix::{signal, SignalKind}; use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::Mutex; use tokio::sync::Mutex;
@ -23,6 +27,9 @@ mod storage;
mod tags; mod tags;
mod utils; mod utils;
use config::Config;
use storage::Storage;
type Context<'a> = poise::Context<'a, Data, Report>; type Context<'a> = poise::Context<'a, Data, Report>;
#[derive(Clone)] #[derive(Clone)]
@ -51,11 +58,20 @@ async fn setup(
_ready: &serenity::Ready, _ready: &serenity::Ready,
framework: &Framework<Data, Report>, framework: &Framework<Data, Report>,
) -> Result<Data> { ) -> 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?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
info!("Registered global commands!"); info!("Registered global commands!");
let data = Data::new()?;
Ok(data) Ok(data)
} }

View file

@ -13,7 +13,7 @@ const LAUNCHER_VERSION_KEY: &str = "launcher-version-v1";
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Storage { pub struct Storage {
client: Client, pub client: Client,
} }
impl Storage { impl Storage {