diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 108 |
1 files changed, 108 insertions, 0 deletions
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 @@ | |||
| 1 | use std::fmt; | ||
| 2 | |||
| 3 | #[derive(Debug)] | ||
| 4 | pub enum Error { | ||
| 5 | Sqlx(sqlx::Error), | ||
| 6 | Reqwest(reqwest::Error), | ||
| 7 | Serenity(serenity::Error), | ||
| 8 | SongbirdJoin(songbird::error::JoinError), | ||
| 9 | SongbirdControl(songbird::tracks::ControlError), | ||
| 10 | Io(std::io::Error), | ||
| 11 | Cron(cron::error::Error), | ||
| 12 | Image(image::ImageError), | ||
| 13 | Qr(qrcode::types::QrError), | ||
| 14 | Scraper(String), | ||
| 15 | Config(String), | ||
| 16 | TaskJoin(String), | ||
| 17 | Custom(String), | ||
| 18 | } | ||
| 19 | |||
| 20 | impl fmt::Display for Error { | ||
| 21 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 22 | use crate::lang; | ||
| 23 | match self { | ||
| 24 | Self::Sqlx(e) => write!(f, "{}: {e}", lang::L_ERR_DATABASE), | ||
| 25 | Self::Reqwest(e) => write!(f, "{}: {e}", lang::L_ERR_HTTP), | ||
| 26 | Self::Serenity(e) => write!(f, "{}: {e}", lang::L_ERR_DISCORD), | ||
| 27 | Self::SongbirdJoin(e) => write!(f, "{}: {e}", lang::L_ERR_VOICE_JOIN), | ||
| 28 | Self::SongbirdControl(e) => write!(f, "{}: {e}", lang::L_ERR_VOICE_CONTROL), | ||
| 29 | Self::Io(e) => write!(f, "{}: {e}", lang::L_ERR_IO), | ||
| 30 | Self::Cron(e) => write!(f, "{}: {e}", lang::L_ERR_CRON), | ||
| 31 | Self::Image(e) => write!(f, "{}: {e}", lang::L_ERR_IMAGE), | ||
| 32 | Self::Qr(e) => write!(f, "{}: {e}", lang::L_ERR_QR), | ||
| 33 | Self::Scraper(e) => write!(f, "{}: {e}", lang::L_ERR_SCRAPER), | ||
| 34 | Self::Config(e) => write!(f, "{}: {e}", lang::L_ERR_CONFIG), | ||
| 35 | Self::TaskJoin(e) => write!(f, "{}: {e}", lang::L_ERR_TASK_JOIN), | ||
| 36 | Self::Custom(e) => write!(f, "{e}"), | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | impl std::error::Error for Error { | ||
| 42 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
| 43 | match self { | ||
| 44 | Self::Sqlx(e) => Some(e), | ||
| 45 | Self::Reqwest(e) => Some(e), | ||
| 46 | Self::Serenity(e) => Some(e), | ||
| 47 | Self::SongbirdJoin(e) => Some(e), | ||
| 48 | Self::SongbirdControl(e) => Some(e), | ||
| 49 | Self::Io(e) => Some(e), | ||
| 50 | Self::Image(e) => Some(e), | ||
| 51 | Self::Qr(e) => Some(e), | ||
| 52 | _ => None, | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | impl From<sqlx::Error> for Error { | ||
| 58 | fn from(e: sqlx::Error) -> Self { | ||
| 59 | Self::Sqlx(e) | ||
| 60 | } | ||
| 61 | } | ||
| 62 | impl From<reqwest::Error> for Error { | ||
| 63 | fn from(e: reqwest::Error) -> Self { | ||
| 64 | Self::Reqwest(e) | ||
| 65 | } | ||
| 66 | } | ||
| 67 | impl From<serenity::Error> for Error { | ||
| 68 | fn from(e: serenity::Error) -> Self { | ||
| 69 | Self::Serenity(e) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | impl From<songbird::error::JoinError> for Error { | ||
| 73 | fn from(e: songbird::error::JoinError) -> Self { | ||
| 74 | Self::SongbirdJoin(e) | ||
| 75 | } | ||
| 76 | } | ||
| 77 | impl From<songbird::tracks::ControlError> for Error { | ||
| 78 | fn from(e: songbird::tracks::ControlError) -> Self { | ||
| 79 | Self::SongbirdControl(e) | ||
| 80 | } | ||
| 81 | } | ||
| 82 | impl From<std::io::Error> for Error { | ||
| 83 | fn from(e: std::io::Error) -> Self { | ||
| 84 | Self::Io(e) | ||
| 85 | } | ||
| 86 | } | ||
| 87 | impl From<cron::error::Error> for Error { | ||
| 88 | fn from(e: cron::error::Error) -> Self { | ||
| 89 | Self::Cron(e) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | impl From<image::ImageError> for Error { | ||
| 93 | fn from(e: image::ImageError) -> Self { | ||
| 94 | Self::Image(e) | ||
| 95 | } | ||
| 96 | } | ||
| 97 | impl From<qrcode::types::QrError> for Error { | ||
| 98 | fn from(e: qrcode::types::QrError) -> Self { | ||
| 99 | Self::Qr(e) | ||
| 100 | } | ||
| 101 | } | ||
| 102 | impl From<tokio::task::JoinError> for Error { | ||
| 103 | fn from(e: tokio::task::JoinError) -> Self { | ||
| 104 | Self::TaskJoin(e.to_string()) | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | pub type Result<T> = std::result::Result<T, Error>; | ||
