api: Stop storing the http::Response in ResponseDeserializationError
This commit is contained in:
parent
d189c5ea29
commit
effb53444d
@ -52,22 +52,20 @@ impl Response {
|
||||
path: syn::Path { segments, .. }, ..
|
||||
}) if segments.last().unwrap().ident == "Option" => {
|
||||
quote! {
|
||||
#field_name: #ruma_api::try_deserialize!(
|
||||
response,
|
||||
#field_name: {
|
||||
headers.remove(#http::header::#header_name)
|
||||
.map(|h| h.to_str().map(|s| s.to_owned()))
|
||||
.transpose()
|
||||
)
|
||||
.transpose()?
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => quote! {
|
||||
#field_name: #ruma_api::try_deserialize!(
|
||||
response,
|
||||
#field_name: {
|
||||
headers.remove(#http::header::#header_name)
|
||||
.expect("response missing expected header")
|
||||
.to_str()
|
||||
)
|
||||
.to_str()?
|
||||
.to_owned()
|
||||
}
|
||||
},
|
||||
};
|
||||
quote_spanned! {span=> #optional_header }
|
||||
@ -228,10 +226,7 @@ impl Response {
|
||||
body => body,
|
||||
};
|
||||
|
||||
#ruma_api::try_deserialize!(
|
||||
response,
|
||||
#serde_json::from_slice(json),
|
||||
)
|
||||
#serde_json::from_slice(json)?
|
||||
};
|
||||
}
|
||||
} else {
|
||||
|
@ -15,9 +15,9 @@ pub enum Void {}
|
||||
|
||||
impl EndpointError for Void {
|
||||
fn try_from_response(
|
||||
response: http::Response<Vec<u8>>,
|
||||
_response: http::Response<Vec<u8>>,
|
||||
) -> Result<Self, ResponseDeserializationError> {
|
||||
Err(ResponseDeserializationError::from_response(response))
|
||||
Err(ResponseDeserializationError::none())
|
||||
}
|
||||
}
|
||||
|
||||
@ -127,28 +127,32 @@ impl<E> From<ResponseDeserializationError> for FromHttpResponseError<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, T> From<T> for FromHttpResponseError<E>
|
||||
where
|
||||
T: Into<DeserializationError>,
|
||||
{
|
||||
fn from(err: T) -> Self {
|
||||
Self::Deserialization(ResponseDeserializationError::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: StdError> StdError for FromHttpResponseError<E> {}
|
||||
|
||||
/// An error that occurred when trying to deserialize a response.
|
||||
#[derive(Debug)]
|
||||
pub struct ResponseDeserializationError {
|
||||
inner: Option<DeserializationError>,
|
||||
http_response: http::Response<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl ResponseDeserializationError {
|
||||
/// Creates a new `ResponseDeserializationError` from the given deserialization error and http
|
||||
/// response.
|
||||
pub fn new(
|
||||
inner: impl Into<DeserializationError>,
|
||||
http_response: http::Response<Vec<u8>>,
|
||||
) -> Self {
|
||||
Self { inner: Some(inner.into()), http_response }
|
||||
pub fn new(inner: impl Into<DeserializationError>) -> Self {
|
||||
Self { inner: Some(inner.into()) }
|
||||
}
|
||||
|
||||
/// Creates a new `ResponseDeserializationError` without an inner deserialization error.
|
||||
pub fn from_response(http_response: http::Response<Vec<u8>>) -> Self {
|
||||
Self { http_response, inner: None }
|
||||
fn none() -> Self {
|
||||
Self { inner: None }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -369,10 +369,4 @@ macro_rules! try_deserialize {
|
||||
Err(err) => return Err($crate::error::RequestDeserializationError::new(err, $kind).into()),
|
||||
}
|
||||
};
|
||||
(@response, $kind:ident, $call:expr) => {
|
||||
match $call {
|
||||
Ok(val) => val,
|
||||
Err(err) => return Err($crate::error::ResponseDeserializationError::new(err, $kind).into()),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -4,11 +4,8 @@ use std::convert::TryFrom;
|
||||
|
||||
use http::{header::CONTENT_TYPE, method::Method};
|
||||
use ruma_api::{
|
||||
error::{
|
||||
FromHttpRequestError, FromHttpResponseError, IntoHttpError, ResponseDeserializationError,
|
||||
ServerError, Void,
|
||||
},
|
||||
try_deserialize, AuthScheme, IncomingRequest, Metadata, OutgoingRequest,
|
||||
error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError, ServerError, Void},
|
||||
try_deserialize, AuthScheme, EndpointError, IncomingRequest, Metadata, OutgoingRequest,
|
||||
};
|
||||
use ruma_identifiers::{RoomAliasId, RoomId};
|
||||
use ruma_serde::Outgoing;
|
||||
@ -109,8 +106,8 @@ impl TryFrom<http::Response<Vec<u8>>> for Response {
|
||||
if http_response.status().as_u16() < 400 {
|
||||
Ok(Response)
|
||||
} else {
|
||||
Err(FromHttpResponseError::Http(ServerError::Unknown(
|
||||
ResponseDeserializationError::from_response(http_response),
|
||||
Err(FromHttpResponseError::Http(ServerError::Known(
|
||||
<Void as EndpointError>::try_from_response(http_response)?,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ impl EndpointError for Error {
|
||||
) -> Result<Self, ResponseDeserializationError> {
|
||||
match from_json_slice::<ErrorBody>(response.body()) {
|
||||
Ok(error_body) => Ok(error_body.into_error(response.status())),
|
||||
Err(de_error) => Err(ResponseDeserializationError::new(de_error, response)),
|
||||
Err(de_error) => Err(ResponseDeserializationError::new(de_error)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user