errors.rs 3.4 KB

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