//! Error conditions. use std::fmt::{self, Debug, Display, Formatter}; use ruma_api::error::{FromHttpResponseError, IntoHttpError}; /// An error that can occur during client operations. #[derive(Debug)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), 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(UrlError), /// Couldn't obtain an HTTP response (e.g. due to network or DNS issues). Response(ResponseError), /// 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(UrlError(err)) => write!(f, "Invalid URL: {}", err), Self::Response(ResponseError(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(UrlError(err.into())) } } #[doc(hidden)] impl From for Error { fn from(err: http::uri::InvalidUriParts) -> Self { Error::Url(UrlError(err.into())) } } #[doc(hidden)] impl From for Error { fn from(err: hyper::Error) -> Self { Error::Response(ResponseError(err)) } } impl From> for Error { fn from(err: FromHttpResponseError) -> Self { Error::FromHttpResponse(err) } } impl std::error::Error for Error {} #[derive(Debug)] pub struct UrlError(http::Error); #[derive(Debug)] pub struct ResponseError(hyper::Error);