aboutsummaryrefslogtreecommitdiff
path: root/src/error/client.rs
blob: 980e3d2d42b30df50bda07be710f37b967062eb8 (plain)
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
pub enum ClientError {
    BadJsonBody(String),
    BadAuthTokenHeader(String),
    UserAlreadyExists { username: String },
    InvalidPassword,
    NotAuthorized,
}

impl ClientError {
    pub fn kind(&self) -> String {
        match self {
            Self::BadJsonBody(..) => "BadJsonBody",
            Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader",
            Self::UserAlreadyExists { .. } => "UserAlreadyExists",
            Self::InvalidPassword => "InvalidPassword",
            Self::NotAuthorized => "NotAuthorized",
        }
        .to_string()
    }

    pub fn into_message(self) -> String {
        match self {
            Self::BadJsonBody(msg) => msg,
            Self::BadAuthTokenHeader(msg) => msg,
            Self::UserAlreadyExists { username } => {
                format!("user with username `{}` already exists", username)
            }
            Self::InvalidPassword => "password is invalid".to_string(),

            Self::NotAuthorized => "user is not authorized".to_string(),
        }
    }
}