wOxlf/src/error.rs

89 lines
2.8 KiB
Rust

use crate::imgur::ImgurError;
use serenity::prelude::SerenityError;
use std::fmt::{Display, Formatter};
use tera::Error;
pub type Result<T> = std::result::Result<T, WoxlfError>;
#[derive(Debug)]
pub enum WoxlfError {
IOError(std::io::Error),
GameStateParseError(toml::de::Error),
GameStateSerializeError(toml::ser::Error),
DurationParseError,
SerenityError(serenity::Error),
DiscordIdParseError(String),
GameNotInProgress,
HostWebhookError,
ImgurError(ImgurError),
RanOutOfCodenames,
TemplateError(tera::Error),
RanOutOfProfilePics,
UnsupportedMsgMedium,
}
impl std::error::Error for WoxlfError {}
impl Display for WoxlfError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let msg = match self {
WoxlfError::IOError(e) => format!("IO Error: {}", e),
WoxlfError::GameStateParseError(e) => format!("Game State Parse Error: {}", e),
WoxlfError::GameStateSerializeError(e) => format!("Game State Serialize Error: {}", e),
WoxlfError::DurationParseError => "Unable to parse duration".to_string(),
WoxlfError::SerenityError(e) => format!("Serenity error: {}", e),
WoxlfError::DiscordIdParseError(e) => format!("Unable to parse player id {}", e),
WoxlfError::GameNotInProgress => "A game is not currently in progress".to_string(),
WoxlfError::HostWebhookError => "Unable to communicate to the host webhook".to_string(),
WoxlfError::ImgurError(err) => format!("Imgur module error: {}", err),
WoxlfError::RanOutOfCodenames => {
"Ran out of codename combinations, add more first/last names to the config"
.to_string()
}
WoxlfError::TemplateError(e) => format!("Template error: {}", e),
WoxlfError::RanOutOfProfilePics => "Ran out of user profile pics".to_string(),
WoxlfError::UnsupportedMsgMedium => {
"Tried to send a message over an unsupported medium".to_string()
}
};
write!(f, "Woxlf Error: {}", msg)
}
}
impl From<SerenityError> for WoxlfError {
fn from(err: SerenityError) -> Self {
Self::SerenityError(err)
}
}
impl From<std::io::Error> for WoxlfError {
fn from(err: std::io::Error) -> Self {
Self::IOError(err)
}
}
impl From<toml::de::Error> for WoxlfError {
fn from(err: toml::de::Error) -> Self {
Self::GameStateParseError(err)
}
}
impl From<toml::ser::Error> for WoxlfError {
fn from(err: toml::ser::Error) -> Self {
Self::GameStateSerializeError(err)
}
}
impl From<ImgurError> for WoxlfError {
fn from(err: ImgurError) -> Self {
Self::ImgurError(err)
}
}
impl From<tera::Error> for WoxlfError {
fn from(err: Error) -> Self {
Self::TemplateError(err)
}
}