api: Remove {Request,Response}DeserializationError

This commit is contained in:
Jonas Platte 2021-10-29 22:44:15 +02:00
parent 5a96cbf322
commit bccbde6f83
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
5 changed files with 16 additions and 50 deletions

View File

@ -1,5 +1,10 @@
# [unreleased]
Breaking changes:
* Remove the `RequestDeserializationError` and `ResponseDeserializationError`
types in favor of using `DeserializationError` directly
# 0.18.5
Bug fixes:

View File

@ -47,7 +47,7 @@ impl OutgoingResponse for MatrixError {
impl EndpointError for MatrixError {
fn try_from_http_response<T: AsRef<[u8]>>(
response: http::Response<T>,
) -> Result<Self, ResponseDeserializationError> {
) -> Result<Self, DeserializationError> {
Ok(Self {
status_code: response.status(),
body: from_json_slice(response.body().as_ref())?,
@ -90,7 +90,7 @@ pub enum IntoHttpError {
pub enum FromHttpRequestError {
/// Deserialization failed
#[error("deserialization failed: {0}")]
Deserialization(RequestDeserializationError),
Deserialization(DeserializationError),
/// HTTP method mismatch
#[error("http method mismatch: expected {expected}, received: {received}")]
@ -103,27 +103,11 @@ pub enum FromHttpRequestError {
}
impl<T> From<T> for FromHttpRequestError
where
T: Into<RequestDeserializationError>,
{
fn from(err: T) -> Self {
Self::Deserialization(err.into())
}
}
/// An error that occurred when trying to deserialize a request.
#[derive(Debug, Error)]
#[error("{inner}")]
pub struct RequestDeserializationError {
inner: DeserializationError,
}
impl<T> From<T> for RequestDeserializationError
where
T: Into<DeserializationError>,
{
fn from(err: T) -> Self {
Self { inner: err.into() }
Self::Deserialization(err.into())
}
}
@ -132,7 +116,7 @@ where
#[non_exhaustive]
pub enum FromHttpResponseError<E> {
/// Deserialization failed
Deserialization(ResponseDeserializationError),
Deserialization(DeserializationError),
/// The server returned a non-success status
Http(ServerError<E>),
@ -155,7 +139,7 @@ impl<E> From<ServerError<E>> for FromHttpResponseError<E> {
impl<E, T> From<T> for FromHttpResponseError<E>
where
T: Into<ResponseDeserializationError>,
T: Into<DeserializationError>,
{
fn from(err: T) -> Self {
Self::Deserialization(err.into())
@ -164,29 +148,6 @@ where
impl<E: StdError> StdError for FromHttpResponseError<E> {}
/// An error that occurred when trying to deserialize a response.
#[derive(Debug)]
pub struct ResponseDeserializationError {
inner: DeserializationError,
}
impl<T> From<T> for ResponseDeserializationError
where
T: Into<DeserializationError>,
{
fn from(err: T) -> Self {
Self { inner: err.into() }
}
}
impl fmt::Display for ResponseDeserializationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl StdError for ResponseDeserializationError {}
/// An error was reported by the server (HTTP status code 4xx or 5xx)
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
@ -196,7 +157,7 @@ pub enum ServerError<E> {
Known(E),
/// An error of unexpected type of structure
Unknown(ResponseDeserializationError),
Unknown(DeserializationError),
}
impl<E: fmt::Display> fmt::Display for ServerError<E> {

View File

@ -362,7 +362,7 @@ pub trait EndpointError: OutgoingResponse + StdError + Sized + Send + 'static {
/// the `ruma_api` macro.
fn try_from_http_response<T: AsRef<[u8]>>(
response: http::Response<T>,
) -> Result<Self, error::ResponseDeserializationError>;
) -> Result<Self, error::DeserializationError>;
}
/// Marker trait for requests that don't require authentication, for the client side.

View File

@ -4,7 +4,7 @@ use std::{collections::BTreeMap, fmt, time::Duration};
use bytes::BufMut;
use ruma_api::{
error::{IntoHttpError, ResponseDeserializationError},
error::{DeserializationError, IntoHttpError},
EndpointError, OutgoingResponse,
};
use ruma_identifiers::RoomVersionId;
@ -215,7 +215,7 @@ pub struct Error {
impl EndpointError for Error {
fn try_from_http_response<T: AsRef<[u8]>>(
response: http::Response<T>,
) -> Result<Self, ResponseDeserializationError> {
) -> Result<Self, DeserializationError> {
let status = response.status();
let error_body: ErrorBody = from_json_slice(response.body().as_ref())?;
Ok(error_body.into_error(status))

View File

@ -6,7 +6,7 @@ use std::{borrow::Cow, fmt};
use bytes::BufMut;
use ruma_api::{
error::{IntoHttpError, ResponseDeserializationError},
error::{DeserializationError, IntoHttpError},
EndpointError, OutgoingResponse,
};
use ruma_common::thirdparty::Medium;
@ -827,7 +827,7 @@ impl From<MatrixError> for UiaaResponse {
impl EndpointError for UiaaResponse {
fn try_from_http_response<T: AsRef<[u8]>>(
response: http::Response<T>,
) -> Result<Self, ResponseDeserializationError> {
) -> Result<Self, DeserializationError> {
if response.status() == http::StatusCode::UNAUTHORIZED {
Ok(UiaaResponse::AuthResponse(from_json_slice(response.body().as_ref())?))
} else {