renegade_sdk/external_match_client/
error.rs

1//! The error type for the external match client
2
3use reqwest::StatusCode;
4
5use crate::http::RelayerHttpClientError;
6
7/// An error that can occur when requesting an external match
8#[derive(Debug, thiserror::Error)]
9pub enum ExternalMatchClientError {
10    /// An error that can occur when requesting an external match
11    #[error(
12        "error while requesting external match: status={}, message={1}",
13        .0.as_ref().map(ToString::to_string).unwrap_or_else(|| "none".to_string())
14    )]
15    Http(Option<StatusCode>, String),
16    /// An error indicating that the api key is invalid
17    #[error("the api key is invalid")]
18    InvalidApiKey,
19    /// An error indicating that the api secret is invalid
20    #[error("the api secret is invalid")]
21    InvalidApiSecret,
22    /// An invalid modification to a malleable match
23    #[error("invalid modification to a malleable match: {0}")]
24    InvalidModification(String),
25    /// An error indicating that an order is invalid
26    #[error("invalid order: {0}")]
27    InvalidOrder(String),
28    /// An error deserializing a response
29    #[error("error deserializing a response: {0}")]
30    Deserialize(String),
31}
32
33impl ExternalMatchClientError {
34    /// Construct a new http error
35    #[allow(clippy::needless_pass_by_value)]
36    pub(crate) fn http<T: ToString>(status: StatusCode, msg: T) -> Self {
37        Self::Http(Some(status), msg.to_string())
38    }
39
40    /// Construct a new invalid modification error
41    #[allow(clippy::needless_pass_by_value)]
42    pub(crate) fn invalid_modification<T: ToString>(msg: T) -> Self {
43        Self::InvalidModification(msg.to_string())
44    }
45
46    /// Construct a new invalid order error
47    #[allow(clippy::needless_pass_by_value)]
48    pub(crate) fn invalid_order<T: ToString>(msg: T) -> Self {
49        Self::InvalidOrder(msg.to_string())
50    }
51
52    /// Construct a new deserialize error
53    #[allow(clippy::needless_pass_by_value)]
54    pub(crate) fn deserialize<T: ToString>(msg: T) -> Self {
55        Self::Deserialize(msg.to_string())
56    }
57}
58
59impl From<reqwest::Error> for ExternalMatchClientError {
60    fn from(err: reqwest::Error) -> Self {
61        Self::Http(None, err.to_string())
62    }
63}
64
65impl From<RelayerHttpClientError> for ExternalMatchClientError {
66    fn from(err: RelayerHttpClientError) -> Self {
67        Self::Http(None, err.to_string())
68    }
69}