use crate::{ byte_trait::FromBytes, request::EventRequest, response::{EventResponse, ResponseBuilder}, }; use bytes::Bytes; use dyn_clone::DynClone; use serde::{Serialize, Serializer}; use std::{fmt, option::NoneError}; use tokio::sync::mpsc::error::SendError; pub trait Error: fmt::Debug + DynClone + Send + Sync { fn as_response(&self) -> EventResponse; } dyn_clone::clone_trait_object!(Error); impl From for DispatchError { fn from(err: T) -> DispatchError { DispatchError { inner: Box::new(err), } } } #[derive(Clone)] pub struct DispatchError { inner: Box, } impl DispatchError { pub fn inner_error(&self) -> &dyn Error { self.inner.as_ref() } } impl fmt::Display for DispatchError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) } } impl fmt::Debug for DispatchError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) } } impl std::error::Error for DispatchError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } fn cause(&self) -> Option<&dyn std::error::Error> { None } } impl From> for DispatchError { fn from(err: SendError) -> Self { InternalError { inner: format!("{}", err), } .into() } } impl From for DispatchError { fn from(s: NoneError) -> Self { InternalError { inner: format!("Unexpected none: {:?}", s), } .into() } } impl From for DispatchError { fn from(s: String) -> Self { InternalError { inner: s }.into() } } impl FromBytes for DispatchError { fn parse_from_bytes(bytes: Bytes) -> Result { let s = String::from_utf8(bytes.to_vec()).unwrap(); Ok(InternalError { inner: s }.into()) } } impl From for EventResponse { fn from(err: DispatchError) -> Self { err.inner_error().as_response() } } #[derive(Clone)] pub(crate) struct InternalError { inner: T, } impl InternalError { pub fn new(inner: T) -> Self { InternalError { inner } } } impl fmt::Debug for InternalError where T: fmt::Debug + 'static + Clone + Send + Sync, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.inner, f) } } impl fmt::Display for InternalError where T: fmt::Debug + fmt::Display + 'static + Clone + Send + Sync, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.inner, f) } } impl Error for InternalError where T: fmt::Debug + fmt::Display + 'static + Clone + Send + Sync, { fn as_response(&self) -> EventResponse { let error = format!("{}", self.inner).into_bytes(); ResponseBuilder::Err().data(error).build() } } impl Serialize for DispatchError { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where S: Serializer, { serializer.serialize_str(&format!("{}", self)) } }