xapi_shared/rest/
error.rs

1use governor::InsufficientCapacity;
2use serde::de::DeserializeOwned;
3
4#[derive(Debug, thiserror::Error)]
5pub enum SharedRestError<Err: SharedRestClientErrorTrait> {
6    #[error("ratelimiter error: {0}")]
7    RatelimiterError(#[from] InsufficientCapacity),
8    #[error("serde error: {0}")]
9    SerdeError(String),
10    #[error("request error: {0}")]
11    RequestError(#[from] reqwest::Error),
12    #[error("client error: {0}")]
13    ClientError(Err),
14    #[error("server error: {0}")]
15    ServerError(String),
16}
17
18pub trait SharedRestClientErrorTrait {
19    fn from_body(body: &str) -> Self;
20}
21
22impl<T: DeserializeOwned> SharedRestClientErrorTrait for T {
23    fn from_body(body: &str) -> Self {
24        serde_json::from_str(body).unwrap_or_else(|_| {
25            tracing::error!("Failed to deserialize error body: {}", body);
26            panic!("Failed to deserialize error body")
27        })
28    }
29}