1use thiserror::Error;
7
8#[derive(Error, Debug)]
10pub enum ApiError {
11 #[error("HTTP error {status}: {text}")]
13 Http {
14 status: reqwest::StatusCode,
15 text: String,
16 },
17
18 #[error("Reqwest error: {0}")]
20 Reqwest(#[from] reqwest::Error),
21
22 #[error("JSON error: {0}")]
24 Json(#[from] serde_json::Error),
25
26 #[error("EventSource error: {0}")]
29 EventSource(String),
30
31 #[error("IO error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("{0}")]
37 Other(String),
38
39 #[error("Unknown error")]
41 Unknown,
42}
43
44pub type Result<T> = std::result::Result<T, ApiError>;
46
47impl ApiError {
48 pub fn http_error(status: reqwest::StatusCode, text: impl Into<String>) -> Self {
50 ApiError::Http {
51 status,
52 text: text.into(),
53 }
54 }
55}
56
57impl From<&str> for ApiError {
58 fn from(s: &str) -> Self {
59 ApiError::Other(s.to_string())
60 }
61}
62
63impl From<String> for ApiError {
64 fn from(s: String) -> Self {
65 ApiError::Other(s)
66 }
67}