errors.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. use bytes::Bytes;
  2. use flowy_derive::ProtoBuf;
  3. pub use flowy_user_infra::errors::ErrorCode;
  4. use lib_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: i32,
  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, $code:expr) => {
  18. #[allow(non_snake_case, missing_docs)]
  19. pub fn $name() -> UserError { $code.into() }
  20. };
  21. }
  22. impl UserError {
  23. pub(crate) fn new(code: ErrorCode, msg: &str) -> Self {
  24. Self {
  25. code: code.value(),
  26. msg: msg.to_owned(),
  27. }
  28. }
  29. pub fn context<T: Debug>(mut self, error: T) -> Self {
  30. self.msg = format!("{:?}", error);
  31. self
  32. }
  33. static_user_error!(email_empty, ErrorCode::EmailIsEmpty);
  34. static_user_error!(email_format, ErrorCode::EmailFormatInvalid);
  35. static_user_error!(email_exist, ErrorCode::EmailAlreadyExists);
  36. static_user_error!(password_empty, ErrorCode::PasswordIsEmpty);
  37. static_user_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
  38. static_user_error!(password_forbid_char, ErrorCode::PasswordContainsForbidCharacters);
  39. static_user_error!(password_format, ErrorCode::PasswordFormatInvalid);
  40. static_user_error!(password_not_match, ErrorCode::PasswordNotMatch);
  41. static_user_error!(name_too_long, ErrorCode::UserNameTooLong);
  42. static_user_error!(name_forbid_char, ErrorCode::UserNameContainForbiddenCharacters);
  43. static_user_error!(name_empty, ErrorCode::UserNameIsEmpty);
  44. static_user_error!(user_id, ErrorCode::UserIdInvalid);
  45. static_user_error!(unauthorized, ErrorCode::UserUnauthorized);
  46. static_user_error!(user_not_exist, ErrorCode::UserNotExist);
  47. static_user_error!(internal, ErrorCode::InternalError);
  48. }
  49. impl std::convert::From<ErrorCode> for UserError {
  50. fn from(code: ErrorCode) -> Self {
  51. UserError {
  52. code: code.value(),
  53. msg: format!("{}", code),
  54. }
  55. }
  56. }
  57. impl std::convert::From<flowy_database::Error> for UserError {
  58. fn from(error: flowy_database::Error) -> Self {
  59. match error {
  60. flowy_database::Error::NotFound => UserError::user_not_exist().context(error),
  61. _ => UserError::internal().context(error),
  62. }
  63. }
  64. }
  65. impl std::convert::From<::r2d2::Error> for UserError {
  66. fn from(error: r2d2::Error) -> Self { UserError::internal().context(error) }
  67. }
  68. impl std::convert::From<lib_ws::errors::WsError> for UserError {
  69. fn from(error: lib_ws::errors::WsError) -> Self {
  70. match error.code {
  71. lib_ws::errors::ErrorCode::InternalError => UserError::internal().context(error.msg),
  72. _ => UserError::internal().context(error),
  73. }
  74. }
  75. }
  76. // use diesel::result::{Error, DatabaseErrorKind};
  77. // use lib_sqlite::ErrorKind;
  78. impl std::convert::From<lib_sqlite::Error> for UserError {
  79. fn from(error: lib_sqlite::Error) -> Self { UserError::internal().context(error) }
  80. }
  81. impl std::convert::From<backend_service::errors::ServerError> for UserError {
  82. fn from(error: backend_service::errors::ServerError) -> Self {
  83. let (code, msg) = server_error_to_user_error(error);
  84. UserError::new(code, &msg)
  85. }
  86. }
  87. use backend_service::errors::ErrorCode as ServerErrorCode;
  88. fn server_error_to_user_error(error: backend_service::errors::ServerError) -> (ErrorCode, String) {
  89. let code = match error.code {
  90. ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
  91. ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
  92. ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
  93. ServerErrorCode::ConnectRefused | ServerErrorCode::ConnectTimeout | ServerErrorCode::ConnectClose => {
  94. ErrorCode::ServerError
  95. },
  96. _ => ErrorCode::InternalError,
  97. };
  98. if code != ErrorCode::InternalError {
  99. let msg = format!("{}", &code);
  100. (code, msg)
  101. } else {
  102. (code, error.msg)
  103. }
  104. }
  105. impl lib_dispatch::Error for UserError {
  106. fn as_response(&self) -> EventResponse {
  107. let bytes: Bytes = self.clone().try_into().unwrap();
  108. ResponseBuilder::Err().data(bytes).build()
  109. }
  110. }