Skip to main content

vn_core/
error.rs

1use reqwest::StatusCode;
2use strum::EnumIs;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[non_exhaustive]
7#[derive(Debug, EnumIs, thiserror::Error)]
8pub enum Error {
9  #[error("Client disconnected")]
10  Disconnected,
11
12  #[error("\"{0}\" is not a valid id")]
13  InvalidId(String),
14
15  #[error("Failed to parse JSON: {0}")]
16  Json(#[from] serde_json::Error),
17
18  #[error("{}", reqwest_error(*status, reason))]
19  RequestFailed {
20    status: Option<StatusCode>,
21    reason: String,
22  },
23
24  #[error("Unauthorized: token needed")]
25  Unauthorized,
26}
27
28impl From<reqwest::Error> for Error {
29  fn from(error: reqwest::Error) -> Self {
30    let status = error.status();
31    let reason = error.to_string();
32    Self::RequestFailed { status, reason }
33  }
34}
35
36fn reqwest_error(status: Option<StatusCode>, reason: &str) -> String {
37  match status {
38    Some(status) => format!("[{status}] {reason}"),
39    None => format!("request failed: {reason}"),
40  }
41}