http-query 0.2.0

A client and server SDK for the HTTP QUERY method (RFC 10008).
Documentation
//! Error and result types.

use std::fmt;

/// A specialized `Result` for this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Errors returned by the `http-query` client and helpers.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// The server returned a non-2xx status code.
    Status {
        /// The HTTP status code.
        status: u16,
        /// The response body (as text, best-effort).
        body: String,
    },
    /// The response advertised no result resource (no `Content-Location`).
    NoQueryResult,
    /// The response advertised no equivalent resource (no `Location`).
    NoEquivalentResource,
    /// The redirect limit was exceeded while following a QUERY (RFC 10008 ยง2.5).
    TooManyRedirects,
    /// An invalid URL or header value.
    Invalid(String),
    /// A transport-level error from the underlying HTTP client.
    #[cfg(feature = "client")]
    Transport(reqwest::Error),
    /// A JSON (de)serialization error.
    #[cfg(feature = "client")]
    Json(serde_json::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Status { status, .. } => write!(f, "server returned status {status}"),
            Error::NoQueryResult => f.write_str("response has no query result location"),
            Error::NoEquivalentResource => {
                f.write_str("response has no equivalent resource location")
            }
            Error::TooManyRedirects => f.write_str("too many redirects"),
            Error::Invalid(msg) => write!(f, "invalid input: {msg}"),
            #[cfg(feature = "client")]
            Error::Transport(err) => write!(f, "transport error: {err}"),
            #[cfg(feature = "client")]
            Error::Json(err) => write!(f, "json error: {err}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            #[cfg(feature = "client")]
            Error::Transport(err) => Some(err),
            #[cfg(feature = "client")]
            Error::Json(err) => Some(err),
            _ => None,
        }
    }
}

#[cfg(feature = "client")]
impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::Transport(err)
    }
}

#[cfg(feature = "client")]
impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Error::Json(err)
    }
}