arxivis 1.0.0

Official Rust SDK for the Arxivis document store
Documentation
use thiserror::Error;

/// All errors that the Arxivis SDK can return.
#[derive(Error, Debug)]
pub enum ArxivisError {
    /// The server returned a non-2xx HTTP status.
    /// The `status` field holds the numeric status code and `message` holds the
    /// `"error"` field from the JSON body (or the HTTP reason phrase when the
    /// body is not valid JSON).
    #[error("HTTP {status}: {message}")]
    Api { status: u16, message: String },

    /// A lower-level reqwest / network error.
    #[error("HTTP client error: {0}")]
    Http(#[from] reqwest::Error),

    /// Failed to (de-)serialize JSON.
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

impl ArxivisError {
    /// Returns `true` when the server returned 404 Not Found.
    pub fn is_not_found(&self) -> bool {
        matches!(self, Self::Api { status: 404, .. })
    }

    /// Returns `true` when the server returned 401 Unauthorized.
    pub fn is_unauthorized(&self) -> bool {
        matches!(self, Self::Api { status: 401, .. })
    }

    /// Returns `true` when the server returned 429 Too Many Requests.
    pub fn is_rate_limited(&self) -> bool {
        matches!(self, Self::Api { status: 429, .. })
    }

    /// Returns the numeric HTTP status code if this is an [`ArxivisError::Api`]
    /// variant, or `None` otherwise.
    pub fn status_code(&self) -> Option<u16> {
        match self {
            Self::Api { status, .. } => Some(*status),
            _ => None,
        }
    }
}