@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 //! Errors surfaced by the sigit.si API client.
2
3 /// The API's error body — `Api::BaseController#render_error` renders every
4 /// failure as `{ "error_code": <i32>, "message": <string> }`.
5 #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
6 pub struct ErrorResponse {
7 pub error_code: i32,
8 pub message: String,
9 }
10
11 /// `error_codes::ErrorCode` values the server sends, mirrored from
12 /// `Api::BaseController`.
13 pub mod codes {
14 pub const UNKNOWN: i32 = 0;
15 pub const UNAUTHORIZED: i32 = 100;
16 pub const INVALID: i32 = 101;
17 }
18
19 #[cfg(feature = "client")]
20 pub type Result<T> = std::result::Result<T, ApiError>;
21
22 #[cfg(feature = "client")]
23 #[derive(Debug, thiserror::Error)]
24 pub enum ApiError {
25 /// The token is missing, expired, or rejected. Callers treat this as
26 /// "clear the stored token and ask the user to sign in again", so it stays
27 /// a distinct variant rather than folding into [`ApiError::Status`].
28 #[error("Your session has expired. Sign in again with `si auth login`.")]
29 Unauthorized,
30
31 /// No token on disk for this environment.
32 #[error("You are not signed in. Run `si auth login` first.")]
33 NotAuthenticated,
34
35 /// The server answered with a non-2xx status and an `ErrorResponse` body.
36 #[error("{message}")]
37 Status {
38 status: u16,
39 error_code: i32,
40 message: String,
41 },
42
43 /// An MCP tool reported an in-band failure (`isError: true`). Issues and
44 /// pull requests go over MCP, so this is a normal user-facing error.
45 #[error("{0}")]
46 Tool(String),
47
48 /// The request never completed — DNS, TLS, connection refused, timeout.
49 #[error("Could not reach {host}: {source}")]
50 Transport {
51 host: String,
52 #[source]
53 source: reqwest::Error,
54 },
55
56 /// A 2xx body that didn't match the expected shape.
57 #[error("Unexpected response from the server: {0}")]
58 Parse(String),
59 }
60
61 #[cfg(feature = "client")]
62 impl ApiError {
63 /// True when the caller should discard the stored credentials.
64 pub fn is_auth_failure(&self) -> bool {
65 matches!(self, ApiError::Unauthorized | ApiError::NotAuthenticated)
66 || matches!(self, ApiError::Status { status: 401, .. })
67 }
68 }