errors.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use bytes::Bytes;
  2. use derive_more::Display;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
  5. use std::{convert::TryInto, fmt, fmt::Debug};
  6. #[derive(Debug, Default, Clone, ProtoBuf)]
  7. pub struct UserError {
  8. #[pb(index = 1)]
  9. pub code: ErrorCode,
  10. #[pb(index = 2)]
  11. pub msg: String,
  12. }
  13. impl std::fmt::Display for UserError {
  14. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
  15. }
  16. macro_rules! static_user_error {
  17. ($name:ident, $status:expr) => {
  18. #[allow(non_snake_case, missing_docs)]
  19. pub fn $name() -> UserError {
  20. UserError {
  21. code: $status,
  22. msg: format!("{}", $status),
  23. }
  24. }
  25. };
  26. }
  27. impl UserError {
  28. pub(crate) fn new(code: ErrorCode, msg: &str) -> Self {
  29. Self {
  30. code,
  31. msg: msg.to_owned(),
  32. }
  33. }
  34. pub(crate) fn code(code: ErrorCode) -> Self {
  35. Self {
  36. msg: format!("{}", &code),
  37. code,
  38. }
  39. }
  40. pub fn context<T: Debug>(mut self, error: T) -> Self {
  41. self.msg = format!("{:?}", error);
  42. self
  43. }
  44. static_user_error!(email_empty, ErrorCode::EmailIsEmpty);
  45. static_user_error!(email_format, ErrorCode::EmailFormatInvalid);
  46. static_user_error!(email_exist, ErrorCode::EmailAlreadyExists);
  47. static_user_error!(password_empty, ErrorCode::PasswordIsEmpty);
  48. static_user_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
  49. static_user_error!(password_forbid_char, ErrorCode::PasswordContainsForbidCharacters);
  50. static_user_error!(password_format, ErrorCode::PasswordFormatInvalid);
  51. static_user_error!(password_not_match, ErrorCode::PasswordNotMatch);
  52. static_user_error!(name_too_long, ErrorCode::UserNameTooLong);
  53. static_user_error!(name_forbid_char, ErrorCode::UserNameContainForbiddenCharacters);
  54. static_user_error!(name_empty, ErrorCode::UserNameIsEmpty);
  55. static_user_error!(user_id, ErrorCode::UserIdInvalid);
  56. static_user_error!(unauthorized, ErrorCode::UserUnauthorized);
  57. static_user_error!(user_not_exist, ErrorCode::UserNotExist);
  58. static_user_error!(internal, ErrorCode::InternalError);
  59. }
  60. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  61. pub enum ErrorCode {
  62. #[display(fmt = "Email can not be empty or whitespace")]
  63. EmailIsEmpty = 0,
  64. #[display(fmt = "Email format is not valid")]
  65. EmailFormatInvalid = 1,
  66. #[display(fmt = "Email already exists")]
  67. EmailAlreadyExists = 2,
  68. #[display(fmt = "Password can not be empty or whitespace")]
  69. PasswordIsEmpty = 10,
  70. #[display(fmt = "Password format too long")]
  71. PasswordTooLong = 11,
  72. #[display(fmt = "Password contains forbidden characters.")]
  73. PasswordContainsForbidCharacters = 12,
  74. #[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
  75. PasswordFormatInvalid = 13,
  76. #[display(fmt = "Password not match")]
  77. PasswordNotMatch = 14,
  78. #[display(fmt = "User name is too long")]
  79. UserNameTooLong = 20,
  80. #[display(fmt = "User name contain forbidden characters")]
  81. UserNameContainForbiddenCharacters = 21,
  82. #[display(fmt = "User name can not be empty or whitespace")]
  83. UserNameIsEmpty = 22,
  84. #[display(fmt = "User id is invalid")]
  85. UserIdInvalid = 23,
  86. #[display(fmt = "User token is invalid")]
  87. UserUnauthorized = 24,
  88. #[display(fmt = "User not exist")]
  89. UserNotExist = 25,
  90. #[display(fmt = "Server is offline, try again later")]
  91. ServerOffline = 26,
  92. #[display(fmt = "Internal error")]
  93. InternalError = 100,
  94. }
  95. impl std::convert::Into<UserError> for ErrorCode {
  96. fn into(self) -> UserError { UserError::new(self, "") }
  97. }
  98. impl std::default::Default for ErrorCode {
  99. fn default() -> Self { ErrorCode::InternalError }
  100. }
  101. impl std::convert::From<flowy_database::Error> for UserError {
  102. fn from(error: flowy_database::Error) -> Self {
  103. match error {
  104. flowy_database::Error::NotFound => UserError::user_not_exist().context(error),
  105. _ => UserError::internal().context(error),
  106. }
  107. }
  108. }
  109. impl std::convert::From<::r2d2::Error> for UserError {
  110. fn from(error: r2d2::Error) -> Self { UserError::internal().context(error) }
  111. }
  112. impl std::convert::From<flowy_ws::errors::WsError> for UserError {
  113. fn from(error: flowy_ws::errors::WsError) -> Self {
  114. match error.code {
  115. flowy_ws::errors::ErrorCode::InternalError => UserError::internal().context(error.msg),
  116. _ => UserError::internal().context(error),
  117. }
  118. }
  119. }
  120. // use diesel::result::{Error, DatabaseErrorKind};
  121. // use flowy_sqlite::ErrorKind;
  122. impl std::convert::From<flowy_sqlite::Error> for UserError {
  123. fn from(error: flowy_sqlite::Error) -> Self { UserError::internal().context(error) }
  124. }
  125. impl std::convert::From<flowy_net::errors::ServerError> for UserError {
  126. fn from(error: flowy_net::errors::ServerError) -> Self {
  127. let (code, msg) = server_error_to_user_error(error);
  128. UserError::new(code, &msg)
  129. }
  130. }
  131. use flowy_net::errors::ErrorCode as ServerErrorCode;
  132. fn server_error_to_user_error(error: flowy_net::errors::ServerError) -> (ErrorCode, String) {
  133. let code = match error.code {
  134. ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
  135. ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
  136. ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
  137. ServerErrorCode::ConnectRefused | ServerErrorCode::ConnectTimeout | ServerErrorCode::ConnectClose => ErrorCode::ServerOffline,
  138. _ => ErrorCode::InternalError,
  139. };
  140. if code != ErrorCode::InternalError {
  141. let msg = format!("{}", &code);
  142. (code, msg)
  143. } else {
  144. (code, error.msg)
  145. }
  146. }
  147. impl flowy_dispatch::Error for UserError {
  148. fn as_response(&self) -> EventResponse {
  149. let bytes: Bytes = self.clone().try_into().unwrap();
  150. ResponseBuilder::Err().data(bytes).build()
  151. }
  152. }