//! Error conditions. use std::fmt::{self, Debug, Display, Formatter}; use ruma_common::api::error::{FromHttpResponseError, IntoHttpError}; /// An error that can occur during client operations. #[derive(Debug)] #[non_exhaustive] pub enum Error { /// Queried endpoint requires authentication but was called on an anonymous client. AuthenticationRequired, /// Construction of the HTTP request failed (this should never happen). IntoHttp(IntoHttpError), /// The request's URL is invalid (this should never happen). Url(http::Error), /// Couldn't obtain an HTTP response (e.g. due to network or DNS issues). Response(E), /// Converting the HTTP response to one of ruma's types failed. FromHttpResponse(FromHttpResponseError), } impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::AuthenticationRequired => { write!(f, "The queried endpoint requires authentication but was called with an anonymous client.") } Self::IntoHttp(err) => write!(f, "HTTP request construction failed: {err}"), Self::Url(err) => write!(f, "Invalid URL: {err}"), Self::Response(err) => write!(f, "Couldn't obtain a response: {err}"), Self::FromHttpResponse(err) => write!(f, "HTTP response conversion failed: {err}"), } } } impl From for Error { fn from(err: IntoHttpError) -> Self { Error::IntoHttp(err) } } #[doc(hidden)] impl From for Error { fn from(err: http::uri::InvalidUri) -> Self { Error::Url(err.into()) } } #[doc(hidden)] impl From for Error { fn from(err: http::uri::InvalidUriParts) -> Self { Error::Url(err.into()) } } impl From> for Error { fn from(err: FromHttpResponseError) -> Self { Error::FromHttpResponse(err) } } impl std::error::Error for Error {}