diff --git a/ruma-api-macros/src/api/response.rs b/ruma-api-macros/src/api/response.rs index 5a795ba2..9eed7f89 100644 --- a/ruma-api-macros/src/api/response.rs +++ b/ruma-api-macros/src/api/response.rs @@ -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_owned() + .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 { diff --git a/ruma-api/src/error.rs b/ruma-api/src/error.rs index 101151cd..bd76272b 100644 --- a/ruma-api/src/error.rs +++ b/ruma-api/src/error.rs @@ -15,9 +15,9 @@ pub enum Void {} impl EndpointError for Void { fn try_from_response( - response: http::Response>, + _response: http::Response>, ) -> Result { - Err(ResponseDeserializationError::from_response(response)) + Err(ResponseDeserializationError::none()) } } @@ -127,28 +127,32 @@ impl From for FromHttpResponseError { } } +impl From for FromHttpResponseError +where + T: Into, +{ + fn from(err: T) -> Self { + Self::Deserialization(ResponseDeserializationError::new(err)) + } +} + impl StdError for FromHttpResponseError {} /// An error that occurred when trying to deserialize a response. #[derive(Debug)] pub struct ResponseDeserializationError { inner: Option, - http_response: http::Response>, } impl ResponseDeserializationError { /// Creates a new `ResponseDeserializationError` from the given deserialization error and http /// response. - pub fn new( - inner: impl Into, - http_response: http::Response>, - ) -> Self { - Self { inner: Some(inner.into()), http_response } + pub fn new(inner: impl Into) -> Self { + Self { inner: Some(inner.into()) } } - /// Creates a new `ResponseDeserializationError` without an inner deserialization error. - pub fn from_response(http_response: http::Response>) -> Self { - Self { http_response, inner: None } + fn none() -> Self { + Self { inner: None } } } diff --git a/ruma-api/src/lib.rs b/ruma-api/src/lib.rs index f9940611..223ab666 100644 --- a/ruma-api/src/lib.rs +++ b/ruma-api/src/lib.rs @@ -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()), - } - }; } diff --git a/ruma-api/tests/manual_endpoint_impl.rs b/ruma-api/tests/manual_endpoint_impl.rs index 62f6ad5c..c22633d6 100644 --- a/ruma-api/tests/manual_endpoint_impl.rs +++ b/ruma-api/tests/manual_endpoint_impl.rs @@ -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>> 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( + ::try_from_response(http_response)?, ))) } } diff --git a/ruma-client-api/src/error.rs b/ruma-client-api/src/error.rs index 01fce172..301de0dd 100644 --- a/ruma-client-api/src/error.rs +++ b/ruma-client-api/src/error.rs @@ -207,7 +207,7 @@ impl EndpointError for Error { ) -> Result { match from_json_slice::(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)), } } }