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
|
pub enum ClientError {
NotFound { path: String },
MethodNotAllowed { path: String },
BadJsonBody(String),
BadQueryString(String),
BadAuthTokenHeader(String),
UsernameIsTaken { username: String },
InvalidPassword,
NotAuthorized,
UserNotFound { id: i64 },
NotQueueOwner { id: i64 },
QueueNotFound { id: i64 },
}
impl ClientError {
pub fn kind(&self) -> String {
match self {
Self::NotFound { .. } => "NotFound",
Self::MethodNotAllowed { .. } => "MethodNotAllowed",
Self::BadJsonBody(..) => "BadJsonBody",
Self::BadQueryString(..) => "BadQueryString",
Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader",
Self::UsernameIsTaken { .. } => "UsernameIsTaken",
Self::InvalidPassword => "InvalidPassword",
Self::NotAuthorized => "NotAuthorized",
Self::UserNotFound { .. } => "UserNotFound",
Self::NotQueueOwner { .. } => "NotQueueOwner",
Self::QueueNotFound { .. } => "QueueNotFound",
}
.to_string()
}
pub fn into_message(self) -> String {
match self {
Self::NotFound { path } => format!("endpoint `{}` not found", path),
Self::MethodNotAllowed { path } => {
format!("endpoint `{}` doesn't support this method", path)
}
Self::BadJsonBody(msg) => msg,
Self::BadQueryString(msg) => msg,
Self::BadAuthTokenHeader(msg) => msg,
Self::UsernameIsTaken { username } => {
format!("username `{}` is taken", username)
}
Self::InvalidPassword => format!("password is invalid"),
Self::NotAuthorized => format!("user is not authorized"),
Self::UserNotFound { id } => format!("user with id `{}` not found", id),
Self::NotQueueOwner { id } => {
format!("you are not the owner of the queue with id `{}`", id)
}
Self::QueueNotFound { id } => format!("queue with id `{}` not found", id),
}
}
}
|