xapi-shared 0.0.1

XAPI Shared Library
Documentation
use governor::InsufficientCapacity;
use serde::de::DeserializeOwned;

#[derive(Debug, thiserror::Error)]
pub enum SharedRestError<Err: SharedRestClientErrorTrait> {
    #[error("ratelimiter error: {0}")]
    RatelimiterError(#[from] InsufficientCapacity),
    #[error("serde error: {0}")]
    SerdeError(String),
    #[error("request error: {0}")]
    RequestError(#[from] reqwest::Error),
    #[error("client error: {0}")]
    ClientError(Err),
    #[error("server error: {0}")]
    ServerError(String),
}

pub trait SharedRestClientErrorTrait {
    fn from_body(body: &str) -> Self;
}

impl<T: DeserializeOwned> SharedRestClientErrorTrait for T {
    fn from_body(body: &str) -> Self {
        serde_json::from_str(body).unwrap_or_else(|_| {
            tracing::error!("Failed to deserialize error body: {}", body);
            panic!("Failed to deserialize error body")
        })
    }
}