matrix_oracle/client/
error.rs

1//! Errors that can occur during client-server lookup
2
3/// Errors that can occur during lookup. Refer to [the specification] to see how
4/// the two variants should be handled.
5///
6/// [the spec]: https://matrix.org/docs/spec/client_server/latest#well-known-uri
7#[derive(Debug)]
8pub enum Error {
9	/// Corresponds to the `FAIL_PROMPT` code in the spec.
10	Prompt(reqwest_middleware::Error),
11	/// Corresponds to the `FAIL_ERROR` code in the spec.
12	Fail(FailError),
13}
14
15impl std::error::Error for Error {
16	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
17		match *self {
18			Self::Prompt(ref e) => Some(e),
19			Self::Fail(ref e) => Some(e),
20		}
21	}
22}
23
24impl std::fmt::Display for Error {
25	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26		match self {
27			Self::Prompt(e) => write!(f, "{}", e),
28			Self::Fail(e) => write!(f, "{}", e),
29		}
30	}
31}
32
33impl From<reqwest::Error> for Error {
34	fn from(e: reqwest::Error) -> Self {
35		Error::Prompt(e.into())
36	}
37}
38
39impl From<reqwest_middleware::Error> for Error {
40	fn from(e: reqwest_middleware::Error) -> Self {
41		Error::Prompt(e)
42	}
43}
44
45impl From<url::ParseError> for Error {
46	fn from(e: url::ParseError) -> Self {
47		Error::Fail(FailError::Url(e))
48	}
49}
50
51impl From<FailError> for Error {
52	fn from(e: FailError) -> Self {
53		Error::Fail(e)
54	}
55}
56
57/// Corresponds to the `FAIL_PROMPT` code in the spec.
58#[derive(Debug)]
59pub enum FailError {
60	/// URL parsing error
61	Url(url::ParseError),
62	/// HTTP error
63	Http(reqwest_middleware::Error),
64}
65
66impl std::error::Error for FailError {
67	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
68		match *self {
69			Self::Http(ref e) => Some(e),
70			Self::Url(ref e) => Some(e),
71		}
72	}
73}
74
75impl std::fmt::Display for FailError {
76	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77		match self {
78			Self::Http(e) => write!(f, "{}", e),
79			Self::Url(e) => write!(f, "{}", e),
80		}
81	}
82}
83
84impl From<reqwest::Error> for FailError {
85	fn from(e: reqwest::Error) -> Self {
86		FailError::Http(e.into())
87	}
88}
89
90impl From<reqwest_middleware::Error> for FailError {
91	fn from(e: reqwest_middleware::Error) -> Self {
92		FailError::Http(e)
93	}
94}
95
96impl From<url::ParseError> for FailError {
97	fn from(e: url::ParseError) -> Self {
98		FailError::Url(e)
99	}
100}