1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::{error::Error as StdError, fmt};
use bytes::BufMut;
use serde_json::{from_slice as from_json_slice, Value as JsonValue};
use thiserror::Error;
use super::{EndpointError, MatrixVersion, OutgoingResponse};
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Debug)]
pub struct MatrixError {
pub status_code: http::StatusCode,
pub body: JsonValue,
}
impl fmt::Display for MatrixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] ", self.status_code.as_u16())?;
fmt::Display::fmt(&self.body, f)
}
}
impl StdError for MatrixError {}
impl OutgoingResponse for MatrixError {
fn try_into_http_response<T: Default + BufMut>(
self,
) -> Result<http::Response<T>, IntoHttpError> {
http::Response::builder()
.header(http::header::CONTENT_TYPE, "application/json")
.status(self.status_code)
.body(crate::serde::json_to_buf(&self.body)?)
.map_err(Into::into)
}
}
impl EndpointError for MatrixError {
fn try_from_http_response<T: AsRef<[u8]>>(
response: http::Response<T>,
) -> Result<Self, DeserializationError> {
Ok(Self {
status_code: response.status(),
body: from_json_slice(response.body().as_ref())?,
})
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum IntoHttpError {
#[error("no access token given, but this endpoint requires one")]
NeedsAuthentication,
#[error(
"endpoint was not supported by server-reported versions, \
but no unstable path to fall back to was defined"
)]
NoUnstablePath,
#[error("could not create any path variant for endpoint, as it was removed in version {0}")]
EndpointRemoved(MatrixVersion),
#[error("JSON serialization failed: {0}")]
Json(#[from] serde_json::Error),
#[error("query parameter serialization failed: {0}")]
Query(#[from] crate::serde::urlencoded::ser::Error),
#[error("header serialization failed: {0}")]
Header(#[from] http::header::InvalidHeaderValue),
#[error("HTTP request construction failed: {0}")]
Http(#[from] http::Error),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum FromHttpRequestError {
#[error("deserialization failed: {0}")]
Deserialization(DeserializationError),
#[error("http method mismatch: expected {expected}, received: {received}")]
MethodMismatch {
expected: http::method::Method,
received: http::method::Method,
},
}
impl<T> From<T> for FromHttpRequestError
where
T: Into<DeserializationError>,
{
fn from(err: T) -> Self {
Self::Deserialization(err.into())
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum FromHttpResponseError<E> {
Deserialization(DeserializationError),
Server(ServerError<E>),
}
impl<E> FromHttpResponseError<E> {
pub fn map<F>(
self,
f: impl FnOnce(ServerError<E>) -> ServerError<F>,
) -> FromHttpResponseError<F> {
match self {
Self::Deserialization(d) => FromHttpResponseError::Deserialization(d),
Self::Server(s) => FromHttpResponseError::Server(f(s)),
}
}
}
impl<E, F> FromHttpResponseError<Result<E, F>> {
pub fn transpose(self) -> Result<FromHttpResponseError<E>, F> {
match self {
Self::Deserialization(d) => Ok(FromHttpResponseError::Deserialization(d)),
Self::Server(s) => s.transpose().map(FromHttpResponseError::Server),
}
}
}
impl<E: fmt::Display> fmt::Display for FromHttpResponseError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Deserialization(err) => write!(f, "deserialization failed: {}", err),
Self::Server(err) => write!(f, "the server returned an error: {}", err),
}
}
}
impl<E> From<ServerError<E>> for FromHttpResponseError<E> {
fn from(err: ServerError<E>) -> Self {
Self::Server(err)
}
}
impl<E, T> From<T> for FromHttpResponseError<E>
where
T: Into<DeserializationError>,
{
fn from(err: T) -> Self {
Self::Deserialization(err.into())
}
}
impl<E: StdError> StdError for FromHttpResponseError<E> {}
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
pub enum ServerError<E> {
Known(E),
Unknown(DeserializationError),
}
impl<E> ServerError<E> {
pub fn map<F>(self, f: impl FnOnce(E) -> F) -> ServerError<F> {
match self {
Self::Known(k) => ServerError::Known(f(k)),
Self::Unknown(u) => ServerError::Unknown(u),
}
}
}
impl<E, F> ServerError<Result<E, F>> {
pub fn transpose(self) -> Result<ServerError<E>, F> {
match self {
Self::Known(Ok(k)) => Ok(ServerError::Known(k)),
Self::Known(Err(e)) => Err(e),
Self::Unknown(u) => Ok(ServerError::Unknown(u)),
}
}
}
impl<E: fmt::Display> fmt::Display for ServerError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ServerError::Known(e) => fmt::Display::fmt(e, f),
ServerError::Unknown(res_err) => fmt::Display::fmt(res_err, f),
}
}
}
impl<E: StdError> StdError for ServerError<E> {}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DeserializationError {
#[error(transparent)]
Utf8(#[from] std::str::Utf8Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Query(#[from] crate::serde::urlencoded::de::Error),
#[error(transparent)]
Ident(#[from] crate::IdParseError),
#[error(transparent)]
Header(#[from] HeaderDeserializationError),
}
impl From<std::convert::Infallible> for DeserializationError {
fn from(err: std::convert::Infallible) -> Self {
match err {}
}
}
impl From<http::header::ToStrError> for DeserializationError {
fn from(err: http::header::ToStrError) -> Self {
Self::Header(HeaderDeserializationError::ToStrError(err))
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum HeaderDeserializationError {
#[error("{0}")]
ToStrError(http::header::ToStrError),
#[error("missing header `{0}`")]
MissingHeader(String),
}
#[derive(Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnknownVersionError;
impl fmt::Display for UnknownVersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "version string was unknown")
}
}
impl StdError for UnknownVersionError {}