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

60
discord/src/error.rs Normal file
View file

@ -0,0 +1,60 @@
use std::fmt::Display;
use poise::serenity_prelude as serenity;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
QuestNotFound(u16),
QuestIsPublic(u16),
QuestIsCompleted(u16),
NoContent,
NoChannelOrUser,
BothChannelAndUser,
SerenityError(serenity::Error),
SquadQuestError(squad_quest::error::Error),
}
impl From<serenity::Error> for Error {
fn from(value: serenity::Error) -> Self {
Self::SerenityError(value)
}
}
impl From<squad_quest::error::Error> for Error {
fn from(value: squad_quest::error::Error) -> Self {
Self::SquadQuestError(value)
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::QuestNotFound(u16) => write!(f, "quest #{u16} not found"),
Self::QuestIsPublic(u16) => write!(f, "quest #{u16} is already public"),
Self::QuestIsCompleted(u16) => write!(f, "quest #{u16} is already completed for this user"),
Self::NoContent => write!(f, "no text or attachment was specified"),
Self::NoChannelOrUser => write!(f, "no channel or user was specified"),
Self::BothChannelAndUser => write!(f, "both channel and user was specified"),
Self::SerenityError(_) => write!(f, "discord interaction error"),
Self::SquadQuestError(_) => write!(f, "internal logic error"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::QuestNotFound(_) |
Self::QuestIsPublic(_) |
Self::QuestIsCompleted(_) |
Self::NoContent |
Self::NoChannelOrUser |
Self::BothChannelAndUser => None,
Self::SerenityError(error) => Some(error),
Self::SquadQuestError(error) => Some(error),
}
}
}