Skip to main content

wavekat_platform_client/
error.rs

1//! Public error type for the crate.
2//!
3//! Library convention: typed variants so consumers can `match` on the
4//! failure mode (network vs. HTTP status vs. OAuth state mismatch).
5//! End-user binaries can `?` these into their own `anyhow::Result`
6//! without losing information.
7
8use std::time::Duration;
9
10/// All errors surfaced by the crate.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// The platform returned a non-2xx status. `body` is truncated to a
14    /// reasonable size before being attached.
15    #[error("HTTP {status} {url}: {body}")]
16    Http {
17        status: u16,
18        url: String,
19        body: String,
20    },
21
22    /// Underlying transport failure (DNS, TLS, connection reset, …).
23    #[error("network error: {0}")]
24    Network(#[from] reqwest::Error),
25
26    /// The response body wasn't valid JSON for the expected shape.
27    #[error("decoding response from {url}: {source}")]
28    Decode {
29        url: String,
30        #[source]
31        source: serde_json::Error,
32    },
33
34    /// The OAuth callback returned a `state` value that didn't match
35    /// what we generated. Refusing the token is the only safe move.
36    #[error("OAuth state mismatch — got {actual:?}, expected {expected:?}")]
37    StateMismatch {
38        actual: Option<String>,
39        expected: String,
40    },
41
42    /// The user (or the platform) cancelled the OAuth flow in the
43    /// browser. The `String` carries the platform-supplied reason.
44    #[error("OAuth flow cancelled in browser: {0}")]
45    Cancelled(String),
46
47    /// The OAuth handshake didn't complete within the allotted time.
48    #[error("OAuth handshake timed out after {0:?}")]
49    Timeout(Duration),
50
51    /// Caller-side problem — usually a malformed input (e.g. a token
52    /// that contains bytes we can't put in an HTTP header).
53    #[error("bad request: {0}")]
54    BadRequest(String),
55
56    /// Local I/O failure (loopback bind, socket read/write, …).
57    #[error("I/O: {0}")]
58    Io(#[from] std::io::Error),
59}
60
61pub type Result<T> = std::result::Result<T, Error>;