errors.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. use crate::{
  2. byte_trait::AFPluginFromBytes,
  3. request::AFPluginEventRequest,
  4. response::{AFPluginEventResponse, ResponseBuilder},
  5. };
  6. use bytes::Bytes;
  7. use dyn_clone::DynClone;
  8. use std::fmt;
  9. use tokio::{sync::mpsc::error::SendError, task::JoinError};
  10. pub trait Error: fmt::Debug + DynClone + Send + Sync {
  11. fn as_response(&self) -> AFPluginEventResponse;
  12. }
  13. dyn_clone::clone_trait_object!(Error);
  14. impl<T: Error + 'static> From<T> for DispatchError {
  15. fn from(err: T) -> DispatchError {
  16. DispatchError { inner: Box::new(err) }
  17. }
  18. }
  19. #[derive(Clone)]
  20. pub struct DispatchError {
  21. inner: Box<dyn Error>,
  22. }
  23. impl DispatchError {
  24. pub fn inner_error(&self) -> &dyn Error {
  25. self.inner.as_ref()
  26. }
  27. }
  28. impl fmt::Display for DispatchError {
  29. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  30. write!(f, "{:?}", &self.inner)
  31. }
  32. }
  33. impl fmt::Debug for DispatchError {
  34. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  35. write!(f, "{:?}", &self.inner)
  36. }
  37. }
  38. impl std::error::Error for DispatchError {
  39. fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
  40. None
  41. }
  42. fn cause(&self) -> Option<&dyn std::error::Error> {
  43. None
  44. }
  45. }
  46. impl From<SendError<AFPluginEventRequest>> for DispatchError {
  47. fn from(err: SendError<AFPluginEventRequest>) -> Self {
  48. InternalError::Other(format!("{}", err)).into()
  49. }
  50. }
  51. impl From<String> for DispatchError {
  52. fn from(s: String) -> Self {
  53. InternalError::Other(s).into()
  54. }
  55. }
  56. #[cfg(feature = "use_protobuf")]
  57. impl From<protobuf::ProtobufError> for DispatchError {
  58. fn from(e: protobuf::ProtobufError) -> Self {
  59. InternalError::ProtobufError(format!("{:?}", e)).into()
  60. }
  61. }
  62. impl AFPluginFromBytes for DispatchError {
  63. fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError> {
  64. let s = String::from_utf8(bytes.to_vec()).unwrap();
  65. Ok(InternalError::DeserializeFromBytes(s).into())
  66. }
  67. }
  68. impl From<DispatchError> for AFPluginEventResponse {
  69. fn from(err: DispatchError) -> Self {
  70. err.inner_error().as_response()
  71. }
  72. }
  73. #[cfg(feature = "use_serde")]
  74. impl serde::Serialize for DispatchError {
  75. fn serialize<S>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
  76. where
  77. S: serde::Serializer,
  78. {
  79. serializer.serialize_str(&format!("{}", self))
  80. }
  81. }
  82. #[derive(Clone, Debug)]
  83. pub(crate) enum InternalError {
  84. ProtobufError(String),
  85. UnexpectedNone(String),
  86. DeserializeFromBytes(String),
  87. JoinError(String),
  88. ServiceNotFound(String),
  89. HandleNotFound(String),
  90. Other(String),
  91. }
  92. impl fmt::Display for InternalError {
  93. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  94. match self {
  95. InternalError::ProtobufError(s) => fmt::Display::fmt(&s, f),
  96. InternalError::UnexpectedNone(s) => fmt::Display::fmt(&s, f),
  97. InternalError::DeserializeFromBytes(s) => fmt::Display::fmt(&s, f),
  98. InternalError::JoinError(s) => fmt::Display::fmt(&s, f),
  99. InternalError::ServiceNotFound(s) => fmt::Display::fmt(&s, f),
  100. InternalError::HandleNotFound(s) => fmt::Display::fmt(&s, f),
  101. InternalError::Other(s) => fmt::Display::fmt(&s, f),
  102. }
  103. }
  104. }
  105. impl Error for InternalError {
  106. fn as_response(&self) -> AFPluginEventResponse {
  107. ResponseBuilder::Err().data(self.to_string()).build()
  108. }
  109. }
  110. impl std::convert::From<JoinError> for InternalError {
  111. fn from(e: JoinError) -> Self {
  112. InternalError::JoinError(format!("{}", e))
  113. }
  114. }