errors.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  2. use futures_channel::mpsc::TrySendError;
  3. use std::fmt::Debug;
  4. use strum_macros::Display;
  5. use tokio::sync::oneshot::error::RecvError;
  6. use tokio_tungstenite::tungstenite::{http::StatusCode, Message};
  7. use url::ParseError;
  8. #[derive(Debug, Default, Clone, ProtoBuf)]
  9. pub struct WSError {
  10. #[pb(index = 1)]
  11. pub code: ErrorCode,
  12. #[pb(index = 2)]
  13. pub msg: String,
  14. }
  15. macro_rules! static_ws_error {
  16. ($name:ident, $status:expr) => {
  17. #[allow(non_snake_case, missing_docs)]
  18. pub fn $name() -> WSError {
  19. WSError {
  20. code: $status,
  21. msg: format!("{}", $status),
  22. }
  23. }
  24. };
  25. }
  26. impl WSError {
  27. #[allow(dead_code)]
  28. pub(crate) fn new(code: ErrorCode) -> WSError {
  29. WSError {
  30. code,
  31. msg: "".to_string(),
  32. }
  33. }
  34. pub fn context<T: Debug>(mut self, error: T) -> Self {
  35. self.msg = format!("{:?}", error);
  36. self
  37. }
  38. static_ws_error!(internal, ErrorCode::InternalError);
  39. static_ws_error!(unsupported_message, ErrorCode::UnsupportedMessage);
  40. static_ws_error!(unauthorized, ErrorCode::Unauthorized);
  41. }
  42. pub(crate) fn internal_error<T>(e: T) -> WSError
  43. where
  44. T: std::fmt::Debug,
  45. {
  46. WSError::internal().context(e)
  47. }
  48. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  49. pub enum ErrorCode {
  50. InternalError = 0,
  51. UnsupportedMessage = 1,
  52. Unauthorized = 2,
  53. }
  54. impl std::default::Default for ErrorCode {
  55. fn default() -> Self {
  56. ErrorCode::InternalError
  57. }
  58. }
  59. impl std::convert::From<url::ParseError> for WSError {
  60. fn from(error: ParseError) -> Self {
  61. WSError::internal().context(error)
  62. }
  63. }
  64. impl std::convert::From<protobuf::ProtobufError> for WSError {
  65. fn from(error: protobuf::ProtobufError) -> Self {
  66. WSError::internal().context(error)
  67. }
  68. }
  69. impl std::convert::From<futures_channel::mpsc::TrySendError<Message>> for WSError {
  70. fn from(error: TrySendError<Message>) -> Self {
  71. WSError::internal().context(error)
  72. }
  73. }
  74. impl std::convert::From<RecvError> for WSError {
  75. fn from(error: RecvError) -> Self {
  76. WSError::internal().context(error)
  77. }
  78. }
  79. impl std::convert::From<tokio_tungstenite::tungstenite::Error> for WSError {
  80. fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
  81. match error {
  82. tokio_tungstenite::tungstenite::Error::Http(response) => {
  83. if response.status() == StatusCode::UNAUTHORIZED {
  84. WSError::unauthorized()
  85. } else {
  86. WSError::internal().context(response)
  87. }
  88. }
  89. _ => WSError::internal().context(error),
  90. }
  91. }
  92. }