2023-10-30 12:35:06 +08:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use dyn_clone::DynClone;
|
|
|
|
use tokio::{sync::mpsc::error::SendError, task::JoinError};
|
|
|
|
|
|
|
|
use crate::prelude::AFConcurrent;
|
2021-06-27 15:11:41 +08:00
|
|
|
use crate::{
|
2023-02-13 09:29:49 +08:00
|
|
|
byte_trait::AFPluginFromBytes,
|
|
|
|
request::AFPluginEventRequest,
|
|
|
|
response::{AFPluginEventResponse, ResponseBuilder},
|
2021-06-27 15:11:41 +08:00
|
|
|
};
|
2021-06-25 23:53:13 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
pub trait Error: fmt::Debug + DynClone + AFConcurrent {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn as_response(&self) -> AFPluginEventResponse;
|
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 {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(err: T) -> DispatchError {
|
|
|
|
DispatchError {
|
|
|
|
inner: Box::new(err),
|
2022-01-23 12:14:00 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
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 {
|
2023-02-13 09:29:49 +08:00
|
|
|
inner: Box<dyn Error>,
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn inner_error(&self) -> &dyn Error {
|
|
|
|
self.inner.as_ref()
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl fmt::Display for DispatchError {
|
2023-02-13 09:29:49 +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 {
|
2023-02-13 09:29:49 +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 std::error::Error for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
None
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
fn cause(&self) -> Option<&dyn std::error::Error> {
|
|
|
|
None
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:35:50 +08:00
|
|
|
impl From<SendError<AFPluginEventRequest>> for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(err: SendError<AFPluginEventRequest>) -> Self {
|
|
|
|
InternalError::Other(format!("{}", err)).into()
|
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:20 +08:00
|
|
|
impl From<String> for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(s: String) -> Self {
|
|
|
|
InternalError::Other(s).into()
|
|
|
|
}
|
2021-08-21 12:11:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "use_protobuf")]
|
|
|
|
impl From<protobuf::ProtobufError> for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(e: protobuf::ProtobufError) -> Self {
|
|
|
|
InternalError::ProtobufError(format!("{:?}", e)).into()
|
|
|
|
}
|
2021-06-30 23:11:27 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:35:50 +08:00
|
|
|
impl AFPluginFromBytes for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError> {
|
|
|
|
let s = String::from_utf8(bytes.to_vec()).unwrap();
|
|
|
|
Ok(InternalError::DeserializeFromBytes(s).into())
|
|
|
|
}
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:59:22 +08:00
|
|
|
impl From<DispatchError> for AFPluginEventResponse {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(err: DispatchError) -> Self {
|
|
|
|
err.inner_error().as_response()
|
|
|
|
}
|
2021-06-25 23:53:13 +08:00
|
|
|
}
|
2022-07-06 13:27:33 +08:00
|
|
|
#[cfg(feature = "use_serde")]
|
|
|
|
impl serde::Serialize for DispatchError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn serialize<S>(
|
|
|
|
&self,
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(&format!("{}", self))
|
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-21 12:11:33 +08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) enum InternalError {
|
2023-02-13 09:29:49 +08:00
|
|
|
ProtobufError(String),
|
|
|
|
UnexpectedNone(String),
|
|
|
|
DeserializeFromBytes(String),
|
|
|
|
JoinError(String),
|
|
|
|
ServiceNotFound(String),
|
|
|
|
HandleNotFound(String),
|
|
|
|
Other(String),
|
2021-08-21 12:11:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for InternalError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
InternalError::ProtobufError(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::UnexpectedNone(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::DeserializeFromBytes(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::JoinError(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::ServiceNotFound(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::HandleNotFound(s) => fmt::Display::fmt(&s, f),
|
|
|
|
InternalError::Other(s) => fmt::Display::fmt(&s, f),
|
2021-08-21 12:11:33 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-21 12:11:33 +08:00
|
|
|
impl Error for InternalError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn as_response(&self) -> AFPluginEventResponse {
|
|
|
|
ResponseBuilder::Err().data(self.to_string()).build()
|
|
|
|
}
|
2021-06-27 15:11:41 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2021-08-21 12:11:33 +08:00
|
|
|
impl std::convert::From<JoinError> for InternalError {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn from(e: JoinError) -> Self {
|
|
|
|
InternalError::JoinError(format!("{}", e))
|
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
}
|