Skip to main content

voip_ms/
error.rs

1use std::fmt;
2
3/// Result type returned by all [`Client`](crate::Client) methods.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Errors returned by the voip.ms client.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// Transport or HTTP-level failure.
10    #[error("HTTP error: {0}")]
11    Http(#[from] reqwest::Error),
12
13    /// The response did not contain the expected JSON envelope.
14    #[error("invalid response: {0}")]
15    InvalidResponse(String),
16
17    /// The API responded with a non-`success` status. The contained string is
18    /// the verbatim `status` value, e.g. `invalid_credentials`,
19    /// `missing_method`, `api_not_enabled`.
20    #[error("API status: {0}")]
21    Api(ApiStatus),
22}
23
24/// A non-success status returned by the voip.ms API.
25///
26/// voip.ms surfaces all method-specific error conditions through the `status`
27/// field of the JSON response. The set of values varies per method and is not
28/// stable across versions, so we keep this as a thin wrapper over the wire
29/// string. See the official voip.ms API documentation for per-method status
30/// values.
31#[derive(Debug, Clone, PartialEq, Eq, Hash)]
32pub struct ApiStatus(pub String);
33
34impl ApiStatus {
35    /// The verbatim `status` string from the response.
36    pub fn as_str(&self) -> &str {
37        &self.0
38    }
39}
40
41impl fmt::Display for ApiStatus {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.write_str(&self.0)
44    }
45}
46
47impl From<String> for ApiStatus {
48    fn from(s: String) -> Self {
49        ApiStatus(s)
50    }
51}