2021-06-27 15:11:41 +08:00
|
|
|
use crate::{
|
2021-07-24 18:55:13 +08:00
|
|
|
byte_trait::FromBytes,
|
2021-06-27 15:11:41 +08:00
|
|
|
request::EventRequest,
|
2021-07-25 08:13:59 +08:00
|
|
|
response::{EventResponse, ResponseBuilder},
|
2021-06-27 15:11:41 +08:00
|
|
|
};
|
2021-08-20 22:00:03 +08:00
|
|
|
use bytes::Bytes;
|
2021-06-28 22:56:15 +08:00
|
|
|
use dyn_clone::DynClone;
|
2021-07-01 15:40:26 +08:00
|
|
|
use serde::{Serialize, Serializer};
|
2021-06-27 15:11:41 +08:00
|
|
|
use std::{fmt, option::NoneError};
|
|
|
|
use tokio::sync::mpsc::error::SendError;
|
2021-06-25 23:53:13 +08:00
|
|
|
|
2021-07-11 21:54:55 +08:00
|
|
|
pub trait Error: fmt::Debug + DynClone + Send + Sync {
|
2021-07-24 18:55:13 +08:00
|
|
|
fn as_response(&self) -> EventResponse;
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
dyn_clone::clone_trait_object!(Error);
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl<T: Error + 'static> From<T> for DispatchError {
|
|
|
|
fn from(err: T) -> DispatchError {
|
|
|
|
DispatchError {
|
2021-06-30 23:11:27 +08:00
|
|
|
inner: Box::new(err),
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
#[derive(Clone)]
|
2021-07-10 16:27:20 +08:00
|
|
|
pub struct DispatchError {
|
2021-06-25 23:53:13 +08:00
|
|
|
inner: Box<dyn Error>,
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl DispatchError {
|
2021-06-25 23:53:13 +08:00
|
|
|
pub fn inner_error(&self) -> &dyn Error { self.inner.as_ref() }
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl fmt::Display for DispatchError {
|
2021-07-11 21:54:55 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) }
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl fmt::Debug for DispatchError {
|
2021-06-25 23:53:13 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) }
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl std::error::Error for DispatchError {
|
2021-06-25 23:53:13 +08:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&dyn std::error::Error> { None }
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl From<SendError<EventRequest>> for DispatchError {
|
2021-06-28 22:56:15 +08:00
|
|
|
fn from(err: SendError<EventRequest>) -> Self {
|
|
|
|
InternalError {
|
|
|
|
inner: format!("{}", err),
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl From<NoneError> for DispatchError {
|
2021-06-27 15:11:41 +08:00
|
|
|
fn from(s: NoneError) -> Self {
|
|
|
|
InternalError {
|
|
|
|
inner: format!("Unexpected none: {:?}", s),
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl From<String> for DispatchError {
|
2021-06-30 23:11:27 +08:00
|
|
|
fn from(s: String) -> Self { InternalError { inner: s }.into() }
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:55:13 +08:00
|
|
|
impl FromBytes for DispatchError {
|
2021-08-20 22:00:03 +08:00
|
|
|
fn parse_from_bytes(bytes: Bytes) -> Result<Self, String> {
|
2021-07-24 18:55:13 +08:00
|
|
|
let s = String::from_utf8(bytes.to_vec()).unwrap();
|
|
|
|
Ok(InternalError { inner: s }.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl From<DispatchError> for EventResponse {
|
|
|
|
fn from(err: DispatchError) -> Self { err.inner_error().as_response() }
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
#[derive(Clone)]
|
2021-07-10 16:27:20 +08:00
|
|
|
pub(crate) struct InternalError<T: Clone> {
|
2021-06-27 15:11:41 +08:00
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
impl<T: Clone> InternalError<T> {
|
2021-06-27 15:11:41 +08:00
|
|
|
pub fn new(inner: T) -> Self { InternalError { inner } }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for InternalError<T>
|
|
|
|
where
|
2021-07-02 20:45:51 +08:00
|
|
|
T: fmt::Debug + 'static + Clone + Send + Sync,
|
2021-06-27 15:11:41 +08:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.inner, f) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Display for InternalError<T>
|
|
|
|
where
|
2021-07-02 20:45:51 +08:00
|
|
|
T: fmt::Debug + fmt::Display + 'static + Clone + Send + Sync,
|
2021-06-27 15:11:41 +08:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.inner, f) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Error for InternalError<T>
|
|
|
|
where
|
2021-07-02 20:45:51 +08:00
|
|
|
T: fmt::Debug + fmt::Display + 'static + Clone + Send + Sync,
|
2021-06-27 15:11:41 +08:00
|
|
|
{
|
2021-06-30 23:11:27 +08:00
|
|
|
fn as_response(&self) -> EventResponse {
|
2021-07-24 18:55:13 +08:00
|
|
|
let error = format!("{}", self.inner).into_bytes();
|
|
|
|
ResponseBuilder::Err().data(error).build()
|
2021-06-30 23:11:27 +08:00
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl Serialize for DispatchError {
|
2021-06-29 23:21:25 +08:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(&format!("{}", self))
|
|
|
|
}
|
|
|
|
}
|