diff --git a/rust/astarte-message-hub-proto/src/proto_message_hub.rs b/rust/astarte-message-hub-proto/src/proto_message_hub.rs index c5087bb..499bf08 100644 --- a/rust/astarte-message-hub-proto/src/proto_message_hub.rs +++ b/rust/astarte-message-hub-proto/src/proto_message_hub.rs @@ -25,6 +25,7 @@ use self::{astarte_data_type::Data, astarte_message::Payload}; use serde::Serialize; +use std::fmt::{Display, Formatter}; use uuid::Uuid; include!("astarteplatform.msghub.rs"); @@ -280,6 +281,13 @@ impl MessageHubEvent { }) } + pub fn take_error(self) -> Option { + self.event.and_then(|r| match r { + message_hub_event::Event::Message(_) => None, + message_hub_event::Event::Error(err) => Some(err), + }) + } + pub fn from_error(error: &dyn std::error::Error) -> Self { Self { event: Some(message_hub_event::Event::Error( @@ -317,6 +325,24 @@ impl MessageHubError { } } +impl Display for MessageHubError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}, source: [", self.description)?; + + let Some((first, remaining)) = self.source.split_first() else { + return write!(f, "]"); + }; + + write!(f, "{first}")?; + for s in remaining { + write!(f, ", {s}")?; + } + write!(f, "]")?; + + Ok(()) + } +} + #[cfg(test)] mod test { use std::collections::HashMap;