ruwuma/src/error.rs
2020-02-09 21:48:11 +01:00

71 lines
2.1 KiB
Rust

//! Error conditions.
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use ruma_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(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<'_>) -> FmtResult {
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<IntoHttpError> for Error {
fn from(err: IntoHttpError) -> Self {
Error::IntoHttp(err)
}
}
#[doc(hidden)]
impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Self {
Error::Url(UrlError(err))
}
}
#[doc(hidden)]
impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Self {
Error::Response(ResponseError(err))
}
}
impl From<FromHttpResponseError> for Error {
fn from(err: FromHttpResponseError) -> Self {
Error::FromHttpResponse(err)
}
}
impl StdError for Error {}
#[derive(Debug)]
pub struct UrlError(http::uri::InvalidUri);
#[derive(Debug)]
pub struct ResponseError(hyper::Error);