1use std::fmt::{self, Debug, Display, Formatter};
4
5use as_variant::as_variant;
6use ruma::api::error::{FromHttpResponseError, FromHttpResponseErrorExt as _, IntoHttpError};
7
8#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error<E, F> {
12 AuthenticationRequired,
14
15 IntoHttp(IntoHttpError),
17
18 Url(http::Error),
20
21 Response(E),
23
24 FromHttpResponse(FromHttpResponseError<F>),
26}
27
28impl<E> Error<E, ruma::api::error::Error> {
29 pub fn error_kind(&self) -> Option<&ruma::api::error::ErrorKind> {
32 as_variant!(self, Self::FromHttpResponse)?.error_kind()
33 }
34}
35
36impl<E: Display, F: Display> Display for Error<E, F> {
37 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::AuthenticationRequired => {
40 write!(
41 f,
42 "The queried endpoint requires authentication but was called with an anonymous client."
43 )
44 }
45 Self::IntoHttp(err) => write!(f, "HTTP request construction failed: {err}"),
46 Self::Url(err) => write!(f, "Invalid URL: {err}"),
47 Self::Response(err) => write!(f, "Couldn't obtain a response: {err}"),
48 Self::FromHttpResponse(err) => write!(f, "HTTP response conversion failed: {err}"),
49 }
50 }
51}
52
53impl<E, F> From<IntoHttpError> for Error<E, F> {
54 fn from(err: IntoHttpError) -> Self {
55 Error::IntoHttp(err)
56 }
57}
58
59#[doc(hidden)]
60impl<E, F> From<http::uri::InvalidUri> for Error<E, F> {
61 fn from(err: http::uri::InvalidUri) -> Self {
62 Error::Url(err.into())
63 }
64}
65
66#[doc(hidden)]
67impl<E, F> From<http::uri::InvalidUriParts> for Error<E, F> {
68 fn from(err: http::uri::InvalidUriParts) -> Self {
69 Error::Url(err.into())
70 }
71}
72
73impl<E, F> From<FromHttpResponseError<F>> for Error<E, F> {
74 fn from(err: FromHttpResponseError<F>) -> Self {
75 Error::FromHttpResponse(err)
76 }
77}
78
79impl<E: Debug + Display, F: Debug + Display> std::error::Error for Error<E, F> {}