diff options
Diffstat (limited to 'src/error/client.rs')
| -rw-r--r-- | src/error/client.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/error/client.rs b/src/error/client.rs new file mode 100644 index 0000000..980e3d2 --- /dev/null +++ b/src/error/client.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | pub enum ClientError { | ||
| 2 | BadJsonBody(String), | ||
| 3 | BadAuthTokenHeader(String), | ||
| 4 | UserAlreadyExists { username: String }, | ||
| 5 | InvalidPassword, | ||
| 6 | NotAuthorized, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl ClientError { | ||
| 10 | pub fn kind(&self) -> String { | ||
| 11 | match self { | ||
| 12 | Self::BadJsonBody(..) => "BadJsonBody", | ||
| 13 | Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", | ||
| 14 | Self::UserAlreadyExists { .. } => "UserAlreadyExists", | ||
| 15 | Self::InvalidPassword => "InvalidPassword", | ||
| 16 | Self::NotAuthorized => "NotAuthorized", | ||
| 17 | } | ||
| 18 | .to_string() | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn into_message(self) -> String { | ||
| 22 | match self { | ||
| 23 | Self::BadJsonBody(msg) => msg, | ||
| 24 | Self::BadAuthTokenHeader(msg) => msg, | ||
| 25 | Self::UserAlreadyExists { username } => { | ||
| 26 | format!("user with username `{}` already exists", username) | ||
| 27 | } | ||
| 28 | Self::InvalidPassword => "password is invalid".to_string(), | ||
| 29 | |||
| 30 | Self::NotAuthorized => "user is not authorized".to_string(), | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
