From cd8ed8bc5793985df2c1692072a932ceee517b95 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Jul 2026 02:13:35 +0200 Subject: OPEN THE RUST GATES Signed-off-by: salmonik --- src/error.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..d90d2e9 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,108 @@ +use std::fmt; + +#[derive(Debug)] +pub enum Error { + Sqlx(sqlx::Error), + Reqwest(reqwest::Error), + Serenity(serenity::Error), + SongbirdJoin(songbird::error::JoinError), + SongbirdControl(songbird::tracks::ControlError), + Io(std::io::Error), + Cron(cron::error::Error), + Image(image::ImageError), + Qr(qrcode::types::QrError), + Scraper(String), + Config(String), + TaskJoin(String), + Custom(String), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use crate::lang; + match self { + Self::Sqlx(e) => write!(f, "{}: {e}", lang::L_ERR_DATABASE), + Self::Reqwest(e) => write!(f, "{}: {e}", lang::L_ERR_HTTP), + Self::Serenity(e) => write!(f, "{}: {e}", lang::L_ERR_DISCORD), + Self::SongbirdJoin(e) => write!(f, "{}: {e}", lang::L_ERR_VOICE_JOIN), + Self::SongbirdControl(e) => write!(f, "{}: {e}", lang::L_ERR_VOICE_CONTROL), + Self::Io(e) => write!(f, "{}: {e}", lang::L_ERR_IO), + Self::Cron(e) => write!(f, "{}: {e}", lang::L_ERR_CRON), + Self::Image(e) => write!(f, "{}: {e}", lang::L_ERR_IMAGE), + Self::Qr(e) => write!(f, "{}: {e}", lang::L_ERR_QR), + Self::Scraper(e) => write!(f, "{}: {e}", lang::L_ERR_SCRAPER), + Self::Config(e) => write!(f, "{}: {e}", lang::L_ERR_CONFIG), + Self::TaskJoin(e) => write!(f, "{}: {e}", lang::L_ERR_TASK_JOIN), + Self::Custom(e) => write!(f, "{e}"), + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Sqlx(e) => Some(e), + Self::Reqwest(e) => Some(e), + Self::Serenity(e) => Some(e), + Self::SongbirdJoin(e) => Some(e), + Self::SongbirdControl(e) => Some(e), + Self::Io(e) => Some(e), + Self::Image(e) => Some(e), + Self::Qr(e) => Some(e), + _ => None, + } + } +} + +impl From for Error { + fn from(e: sqlx::Error) -> Self { + Self::Sqlx(e) + } +} +impl From for Error { + fn from(e: reqwest::Error) -> Self { + Self::Reqwest(e) + } +} +impl From for Error { + fn from(e: serenity::Error) -> Self { + Self::Serenity(e) + } +} +impl From for Error { + fn from(e: songbird::error::JoinError) -> Self { + Self::SongbirdJoin(e) + } +} +impl From for Error { + fn from(e: songbird::tracks::ControlError) -> Self { + Self::SongbirdControl(e) + } +} +impl From for Error { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} +impl From for Error { + fn from(e: cron::error::Error) -> Self { + Self::Cron(e) + } +} +impl From for Error { + fn from(e: image::ImageError) -> Self { + Self::Image(e) + } +} +impl From for Error { + fn from(e: qrcode::types::QrError) -> Self { + Self::Qr(e) + } +} +impl From for Error { + fn from(e: tokio::task::JoinError) -> Self { + Self::TaskJoin(e.to_string()) + } +} + +pub type Result = std::result::Result; -- cgit v1.2.3