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
//! The library's error type.
/// Any error, boxed: what event handlers return.
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
/// `Result` with the library's [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Everything that can go wrong.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// An answer the API refused (docs.zynko.org/api/#erros).
#[error("{message} ({status} {code})")]
Api {
/// HTTP status.
status: u16,
/// `bad_request`, `forbidden`, `not_found`, `rate_limited`...
code: String,
message: String,
},
/// The request did not reach the server, or the answer did not come back.
#[error("falha na requisição: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON inválido: {0}")]
Json(#[from] serde_json::Error),
#[error("{0}")]
Io(#[from] std::io::Error),
/// The gateway refused the token, or the client was closed.
#[error("gateway: {0}")]
Gateway(String),
/// Waited too long (see `Client::wait_for`).
#[error("tempo esgotado: {0}")]
Timeout(String),
/// Joining or playing in a voice room.
#[error("voz: {0}")]
Voice(String),
/// An event handler returned an error or panicked.
#[error("erro num handler: {0}")]
Handler(BoxError),
#[error("{0}")]
Other(String),
}
impl Error {
/// The HTTP status, for API errors.
pub fn status(&self) -> Option<u16> {
match self {
Error::Api { status, .. } => Some(*status),
Error::Http(e) => e.status().map(|s| s.as_u16()),
_ => None,
}
}
/// The API's error code (`not_found`, `forbidden`...), for API errors.
pub fn code(&self) -> Option<&str> {
match self {
Error::Api { code, .. } => Some(code),
_ => None,
}
}
}