From 467e7afd5e0148b264c76c606b67b8679ef7c55f Mon Sep 17 00:00:00 2001 From: florianjacob Date: Wed, 12 Aug 2020 14:00:44 +0200 Subject: [PATCH] Require std::error::Error for EndpointError and implement it for ruma_api::error::FromHttpResponseError and Void. This allows integrating EndpointErrors in the common rust error ecosystem like thiserror and anyhow. --- ruma-api/CHANGELOG.md | 6 ++++++ ruma-api/src/error.rs | 10 ++++++++++ ruma-api/src/lib.rs | 2 +- ruma-client-api/src/r0/uiaa.rs | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ruma-api/CHANGELOG.md b/ruma-api/CHANGELOG.md index cc70e558..0b62cdd0 100644 --- a/ruma-api/CHANGELOG.md +++ b/ruma-api/CHANGELOG.md @@ -3,6 +3,12 @@ Breaking changes: * Update strum dependency to 0.19 +* The `EndpointError` trait now requires `std::error::Error`. This allows integrating `EndpointError`s in the common + rust error ecosystem like `thiserror` and `anyhow`. + +Improvements: + +* The `EndpointError`s that come with ruma crates now implement `std::errror::Error`. # 0.17.0 diff --git a/ruma-api/src/error.rs b/ruma-api/src/error.rs index 95223fe1..c118ece2 100644 --- a/ruma-api/src/error.rs +++ b/ruma-api/src/error.rs @@ -17,6 +17,14 @@ impl crate::EndpointError for Void { } } +impl Display for Void { + fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { + match self {} + } +} + +impl std::error::Error for Void {} + /// An error when converting one of ruma's endpoint-specific request or response /// types to the corresponding http type. #[derive(Debug)] @@ -162,6 +170,8 @@ impl From for FromHttpResponseError { } } +impl std::error::Error for FromHttpResponseError {} + /// An error that occurred when trying to deserialize a response. #[derive(Debug)] pub struct ResponseDeserializationError { diff --git a/ruma-api/src/lib.rs b/ruma-api/src/lib.rs index 3f5f308c..426ec064 100644 --- a/ruma-api/src/lib.rs +++ b/ruma-api/src/lib.rs @@ -232,7 +232,7 @@ pub trait Outgoing { } /// Gives users the ability to define their own serializable/deserializable errors. -pub trait EndpointError: Sized { +pub trait EndpointError: std::error::Error + Sized { /// Tries to construct `Self` from an `http::Response`. /// /// This will always return `Err` variant when no `error` field is defined in diff --git a/ruma-client-api/src/r0/uiaa.rs b/ruma-client-api/src/r0/uiaa.rs index ffafb8a6..9b1bcb31 100644 --- a/ruma-client-api/src/r0/uiaa.rs +++ b/ruma-client-api/src/r0/uiaa.rs @@ -119,6 +119,8 @@ impl EndpointError for UiaaResponse { } } +impl std::error::Error for UiaaResponse {} + impl From for http::Response> { fn from(uiaa_response: UiaaResponse) -> http::Response> { match uiaa_response {