122 lines
3.1 KiB
Rust
Raw Normal View History

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,
response::{EventResponse, ResponseBuilder, StatusCode},
2021-06-27 15:11:41 +08:00
};
use dyn_clone::DynClone;
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
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
}
dyn_clone::clone_trait_object!(Error);
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
}
#[derive(Clone)]
pub struct DispatchError {
2021-06-25 23:53:13 +08:00
inner: Box<dyn Error>,
}
impl DispatchError {
2021-06-25 23:53:13 +08:00
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) }
2021-06-25 23:53:13 +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) }
}
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 }
}
impl From<SendError<EventRequest>> for DispatchError {
fn from(err: SendError<EventRequest>) -> Self {
InternalError {
inner: format!("{}", err),
}
.into()
}
2021-06-27 15:11:41 +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()
}
}
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 {
fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, String> {
let s = String::from_utf8(bytes.to_vec()).unwrap();
Ok(InternalError { inner: s }.into())
}
}
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
#[derive(Clone)]
pub(crate) struct InternalError<T: Clone> {
2021-06-27 15:11:41 +08:00
inner: T,
}
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
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
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
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
}
impl Serialize for DispatchError {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}", self))
}
}