Skip to main content

colab_cli/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum ColabError {
5    #[error("not authenticated \u{2014} run `colab-cli auth login` first")]
6    NotAuthenticated,
7
8    #[error("authentication failed: {0}")]
9    AuthFailed(String),
10
11    #[error("token refresh failed: {reason}")]
12    TokenRefreshFailed { reason: String },
13
14    #[error("server not found: {endpoint}")]
15    ServerNotFound { endpoint: String },
16
17    #[error("too many servers assigned \u{2014} remove one first")]
18    TooManyAssignments,
19
20    #[error("insufficient quota to assign this server type")]
21    InsufficientQuota,
22
23    #[error("account blocked from Colab servers due to suspected abuse")]
24    AccountDenylisted,
25
26    #[error("API request failed: {status} {url}{}", body.as_deref().map(|b| format!("\n  body: {b}")).unwrap_or_default())]
27    ApiError {
28        status: u16,
29        url: String,
30        body: Option<String>,
31    },
32
33    #[error("unexpected API response: {0}")]
34    ParseError(String),
35
36    #[error("local config error: {0}")]
37    Config(String),
38
39    #[error("I/O error: {0}")]
40    Io(#[from] std::io::Error),
41
42    #[error("network error: {0}")]
43    Network(#[from] reqwest::Error),
44
45    #[error("JSON error: {0}")]
46    Json(#[from] serde_json::Error),
47
48    #[error("OAuth2 error: {0}")]
49    OAuth(String),
50}
51
52impl ColabError {
53    pub fn api(status: u16, url: impl Into<String>, body: Option<String>) -> Self {
54        Self::ApiError {
55            status,
56            url: url.into(),
57            body,
58        }
59    }
60
61    pub fn parse(msg: impl Into<String>) -> Self {
62        Self::ParseError(msg.into())
63    }
64
65    pub fn config(msg: impl Into<String>) -> Self {
66        Self::Config(msg.into())
67    }
68
69    pub fn oauth(msg: impl Into<String>) -> Self {
70        Self::OAuth(msg.into())
71    }
72}
73
74pub type Result<T> = std::result::Result<T, ColabError>;