Skip to main content

capmonstercloud_client/
error.rs

1//! Error type returned by every fallible operation of the SDK.
2
3use std::time::Duration;
4
5/// Convenient alias: `Result<T>` == `std::result::Result<T, capmonstercloud_client::Error>`.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything that can go wrong while talking to CapMonster Cloud.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// The request object is inconsistent (missing/forbidden field, value out of range...).
12    /// Raised locally, before anything is sent to the network.
13    #[error("invalid request: {0}")]
14    Validation(String),
15
16    /// Network / TLS / timeout error from the HTTP layer.
17    #[error("http error: {0}")]
18    Http(#[from] reqwest::Error),
19
20    /// The server answered with an unexpected HTTP status code.
21    #[error("unexpected HTTP status {status} from {method}")]
22    Status { method: &'static str, status: u16 },
23
24    /// `createTask` returned `errorId != 0`.
25    #[error("cannot create task: [{code}] {description}")]
26    CreateTask { code: String, description: String },
27
28    /// `getTaskResult` returned `errorId != 0`.
29    #[error("cannot get task result: [{code}] {description}")]
30    GetResult { code: String, description: String },
31
32    /// `getBalance` returned `errorId != 0`.
33    #[error("cannot get balance: [{code}] {description}")]
34    GetBalance { code: String, description: String },
35
36    /// The server's JSON was missing a field we need (e.g. `taskId`).
37    #[error("malformed API response: {0}")]
38    MalformedResponse(String),
39
40    /// The solution did not arrive within the maximum waiting interval of this task type.
41    #[error("failed to get a solution within {0:?}")]
42    Timeout(Duration),
43
44    /// The proxy type given in `ClientOptions::client_proxy` is not supported (only http/https).
45    #[error("unsupported client proxy type: {0}")]
46    UnsupportedProxyType(String),
47
48    /// (De)serialization failure.
49    #[error("json error: {0}")]
50    Json(#[from] serde_json::Error),
51}