aboutsummaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: d90d2e97d3d28344bf20d7e25267169f2df5ff07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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<sqlx::Error> for Error {
    fn from(e: sqlx::Error) -> Self {
        Self::Sqlx(e)
    }
}
impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Self::Reqwest(e)
    }
}
impl From<serenity::Error> for Error {
    fn from(e: serenity::Error) -> Self {
        Self::Serenity(e)
    }
}
impl From<songbird::error::JoinError> for Error {
    fn from(e: songbird::error::JoinError) -> Self {
        Self::SongbirdJoin(e)
    }
}
impl From<songbird::tracks::ControlError> for Error {
    fn from(e: songbird::tracks::ControlError) -> Self {
        Self::SongbirdControl(e)
    }
}
impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}
impl From<cron::error::Error> for Error {
    fn from(e: cron::error::Error) -> Self {
        Self::Cron(e)
    }
}
impl From<image::ImageError> for Error {
    fn from(e: image::ImageError) -> Self {
        Self::Image(e)
    }
}
impl From<qrcode::types::QrError> for Error {
    fn from(e: qrcode::types::QrError) -> Self {
        Self::Qr(e)
    }
}
impl From<tokio::task::JoinError> for Error {
    fn from(e: tokio::task::JoinError) -> Self {
        Self::TaskJoin(e.to_string())
    }
}

pub type Result<T> = std::result::Result<T, Error>;