feat: Proper error handling

- Bump version to 0.8.0
- Added discord error struct
- All errors now implement std::error::Error
- Implemented error handler instead of relying on default
- Fixed bug where you could send answer on a completed quest
This commit is contained in:
Alexey 2025-12-12 16:52:31 +03:00
commit 99812c5d7c
14 changed files with 163 additions and 138 deletions

View file

@ -1,6 +1,7 @@
//use poise::{CreateReply, serenity_prelude as serenity};
use std::error::Error as StdError;
use poise::CreateReply;
use crate::{Context, Error};
use crate::{Context, Data, Error};
pub mod quest;
pub mod init;
@ -8,10 +9,30 @@ pub mod answer;
pub mod social;
pub mod account;
pub const ERROR_MSG: &str = "Server error :(";
#[poise::command(prefix_command)]
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
poise::builtins::register_application_commands_buttons(ctx).await?;
Ok(())
}
pub async fn error_handler(error: poise::FrameworkError<'_, Data, Error>) {
eprintln!("ERROR:");
print_error_recursively(&error);
if let Some(ctx) = error.ctx() {
let response = match error {
poise::FrameworkError::Command { error, .. } => format!("Internal server error: {error}"),
_ => format!("Internal server error: {error}"),
};
if let Err(error) = ctx.send(CreateReply::default().content(response).ephemeral(true)).await {
eprintln!("Couldn't send error message: {error}");
}
}
}
fn print_error_recursively(error: &impl StdError) {
eprintln!("{error}");
if let Some(source) = error.source() {
eprintln!("source:");
print_error_recursively(&source);
}
}