diff --git a/crates/ruma-serde/src/base64.rs b/crates/ruma-serde/src/base64.rs index 7ca8e949..e6109663 100644 --- a/crates/ruma-serde/src/base64.rs +++ b/crates/ruma-serde/src/base64.rs @@ -83,8 +83,8 @@ impl Base64 { } /// Parse some base64-encoded data to create a `Base64` instance. - pub fn parse(encoded: impl AsRef<[u8]>) -> Result { - base64::decode_config(encoded, C::CONF.0).map(Self::new) + pub fn parse(encoded: impl AsRef<[u8]>) -> Result { + base64::decode_config(encoded, C::CONF.0).map(Self::new).map_err(Base64DecodeError) } } @@ -118,3 +118,21 @@ impl> Serialize for Base64 { serializer.serialize_str(&self.encode()) } } + +/// An error that occurred while decoding a base64 string. +#[derive(Clone)] +pub struct Base64DecodeError(base64::DecodeError); + +impl fmt::Debug for Base64DecodeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Display for Base64DecodeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl std::error::Error for Base64DecodeError {} diff --git a/crates/ruma-serde/src/lib.rs b/crates/ruma-serde/src/lib.rs index 9df0366a..0b6cdafe 100644 --- a/crates/ruma-serde/src/lib.rs +++ b/crates/ruma-serde/src/lib.rs @@ -22,7 +22,7 @@ pub mod test; pub mod urlencoded; pub use self::{ - base64::Base64, + base64::{Base64, Base64DecodeError}, buf::{json_to_buf, slice_to_buf}, can_be_empty::{is_empty, CanBeEmpty}, canonical_json::{ diff --git a/crates/ruma-signatures/src/error.rs b/crates/ruma-signatures/src/error.rs index 0efdf698..3c986dcb 100644 --- a/crates/ruma-signatures/src/error.rs +++ b/crates/ruma-signatures/src/error.rs @@ -1,4 +1,5 @@ use ruma_identifiers::{EventId, RoomVersionId, ServerName}; +use ruma_serde::Base64DecodeError; use thiserror::Error; /// `ruma-signature`'s error type, wraps a number of other error types. @@ -228,7 +229,7 @@ pub enum ParseError { string: String, /// The originating error. #[source] - source: base64::DecodeError, + source: Base64DecodeError, }, } @@ -254,7 +255,7 @@ impl ParseError { pub(crate) fn base64, T2: Into>( of_type: T1, string: T2, - source: base64::DecodeError, + source: Base64DecodeError, ) -> Error { Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into() }