2021-06-27 15:11:41 +08:00
|
|
|
use crate::{
|
|
|
|
request::EventRequest,
|
2021-07-01 15:40:26 +08:00
|
|
|
response::{EventResponse, ResponseBuilder, StatusCode},
|
2021-06-27 15:11:41 +08:00
|
|
|
};
|
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-02 20:45:51 +08:00
|
|
|
pub trait Error: fmt::Debug + fmt::Display + DynClone + Send + Sync {
|
2021-06-25 23:53:13 +08:00
|
|
|
fn status_code(&self) -> StatusCode;
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
fn as_response(&self) -> EventResponse { EventResponse::new(self.status_code()) }
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
dyn_clone::clone_trait_object!(Error);
|
|
|
|
|
2021-06-25 23:53:13 +08:00
|
|
|
impl<T: Error + 'static> From<T> for SystemError {
|
2021-06-30 23:11:27 +08:00
|
|
|
fn from(err: T) -> SystemError {
|
|
|
|
SystemError {
|
|
|
|
inner: Box::new(err),
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
#[derive(Clone)]
|
2021-06-25 23:53:13 +08:00
|
|
|
pub struct SystemError {
|
|
|
|
inner: Box<dyn Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SystemError {
|
|
|
|
pub fn inner_error(&self) -> &dyn Error { self.inner.as_ref() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SystemError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.inner, f) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for SystemError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self.inner) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for SystemError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&dyn std::error::Error> { None }
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl From<SendError<EventRequest>> for SystemError {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NoneError> for SystemError {
|
|
|
|
fn from(s: NoneError) -> Self {
|
|
|
|
InternalError {
|
|
|
|
inner: format!("Unexpected none: {:?}", s),
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 23:11:27 +08:00
|
|
|
impl From<String> for SystemError {
|
|
|
|
fn from(s: String) -> Self { InternalError { inner: s }.into() }
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl From<SystemError> for EventResponse {
|
2021-06-25 23:53:13 +08:00
|
|
|
fn from(err: SystemError) -> Self { err.inner_error().as_response() }
|
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
|
2021-06-28 22:56:15 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub 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
|
|
|
{
|
|
|
|
fn status_code(&self) -> StatusCode { StatusCode::Err }
|
|
|
|
|
2021-06-30 23:11:27 +08:00
|
|
|
fn as_response(&self) -> EventResponse {
|
|
|
|
let error = InternalError {
|
|
|
|
inner: format!("{}", self.inner),
|
|
|
|
}
|
|
|
|
.into();
|
|
|
|
|
2021-07-01 15:40:26 +08:00
|
|
|
ResponseBuilder::Err().error(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
|
|
|
|
|
|
|
impl Serialize for SystemError {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(&format!("{}", self))
|
|
|
|
}
|
|
|
|
}
|