Skip to main content

bitwarden_api_base/
error.rs

1//! Error types for API operations.
2
3use std::{convert::Infallible, error, fmt, marker::PhantomData};
4
5use serde::{Deserialize, Serialize};
6
7/// Response content from a failed API call.
8#[derive(Debug, Serialize, Deserialize)]
9#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
10pub struct ResponseContent {
11    /// HTTP status code of the response.
12    #[serde(with = "crate::status_code_serializer")]
13    pub status: reqwest::StatusCode,
14    /// Response body content.
15    pub message: String,
16}
17
18/// Errors that can occur during API operations.
19#[derive(Debug)]
20#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
21pub enum Error<T = ()> {
22    /// Error from the reqwest HTTP client.
23    Reqwest(reqwest::Error),
24    /// Error from the reqwest middleware.
25    ReqwestMiddleware(reqwest_middleware::Error),
26    /// JSON serialization/deserialization error.
27    Serde(serde_json::Error),
28    /// I/O error.
29    Io(std::io::Error),
30    /// API returned an error response.
31    Response(ResponseContent),
32
33    /// Phantom variant to keep the unused `T` parameter alive without affecting downstream
34    /// `impl<T> From<Error<T>> for FooError` impls. Uninhabited via [`Infallible`].
35    #[doc(hidden)]
36    _Phantom(PhantomData<T>, Infallible),
37}
38
39impl<T> fmt::Display for Error<T> {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        let (module, e) = match self {
42            Error::Reqwest(e) => ("reqwest", e.to_string()),
43            Error::ReqwestMiddleware(e) => ("reqwest-middleware", e.to_string()),
44            Error::Serde(e) => ("serde", e.to_string()),
45            Error::Io(e) => ("IO", e.to_string()),
46            Error::Response(e) => ("response", format!("status code {}", e.status)),
47            Error::_Phantom(_, _) => unreachable!(),
48        };
49        write!(f, "error in {}: {}", module, e)
50    }
51}
52
53impl<T: fmt::Debug> error::Error for Error<T> {
54    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
55        Some(match self {
56            Error::Reqwest(e) => e,
57            Error::ReqwestMiddleware(e) => e,
58            Error::Serde(e) => e,
59            Error::Io(e) => e,
60            Error::Response(_) | Error::_Phantom(_, _) => return None,
61        })
62    }
63}
64
65impl<T> From<reqwest::Error> for Error<T> {
66    fn from(e: reqwest::Error) -> Self {
67        Error::Reqwest(e)
68    }
69}
70
71impl<T> From<reqwest_middleware::Error> for Error<T> {
72    fn from(e: reqwest_middleware::Error) -> Self {
73        Error::ReqwestMiddleware(e)
74    }
75}
76
77impl<T> From<serde_json::Error> for Error<T> {
78    fn from(e: serde_json::Error) -> Self {
79        Error::Serde(e)
80    }
81}
82
83impl<T> From<std::io::Error> for Error<T> {
84    fn from(e: std::io::Error) -> Self {
85        Error::Io(e)
86    }
87}
88
89impl<T> From<ResponseContent> for Error<T> {
90    fn from(value: ResponseContent) -> Self {
91        Self::Response(value)
92    }
93}