errors.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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::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 UserError {
  14. pub(crate) fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
  15. }
  16. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  17. pub enum ErrorCode {
  18. #[display(fmt = "Unknown")]
  19. Unknown = 0,
  20. #[display(fmt = "Database init failed")]
  21. UserDatabaseInitFailed = 1,
  22. #[display(fmt = "Acquire database write lock failed")]
  23. AcquireWriteLockedFailed = 2,
  24. #[display(fmt = "Acquire database read lock failed")]
  25. AcquireReadLockedFailed = 3,
  26. #[display(fmt = "Opening database is not belonging to the current user")]
  27. UserDatabaseDidNotMatch = 4,
  28. #[display(fmt = "Email can not be empty or whitespace")]
  29. EmailIsEmpty = 20,
  30. #[display(fmt = "Email format is not valid")]
  31. EmailFormatInvalid = 21,
  32. #[display(fmt = "Email already exists")]
  33. EmailAlreadyExists = 22,
  34. #[display(fmt = "Password can not be empty or whitespace")]
  35. PasswordIsEmpty = 30,
  36. #[display(fmt = "Password format too long")]
  37. PasswordTooLong = 31,
  38. #[display(fmt = "Password contains forbidden characters.")]
  39. PasswordContainsForbidCharacters = 32,
  40. #[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
  41. PasswordFormatInvalid = 33,
  42. #[display(fmt = "Password not match")]
  43. PasswordNotMatch = 34,
  44. #[display(fmt = "User name is too long")]
  45. UserNameTooLong = 40,
  46. #[display(fmt = "User name contain forbidden characters")]
  47. ContainForbiddenCharacters = 41,
  48. #[display(fmt = "User name can not be empty or whitespace")]
  49. UserNameIsEmpty = 42,
  50. #[display(fmt = "User workspace is invalid")]
  51. UserWorkspaceInvalid = 50,
  52. #[display(fmt = "User id is invalid")]
  53. UserIdInvalid = 51,
  54. #[display(fmt = "User token is invalid")]
  55. UserUnauthorized = 54,
  56. #[display(fmt = "User not exist")]
  57. UserNotExist = 55,
  58. #[display(fmt = "Internal error")]
  59. InternalError = 100,
  60. }
  61. impl std::default::Default for ErrorCode {
  62. fn default() -> Self { ErrorCode::Unknown }
  63. }
  64. impl std::convert::From<flowy_database::Error> for UserError {
  65. fn from(error: flowy_database::Error) -> Self {
  66. match error {
  67. flowy_database::Error::NotFound => ErrorBuilder::new(ErrorCode::UserNotExist).error(error).build(),
  68. _ => ErrorBuilder::new(ErrorCode::InternalError).error(error).build(),
  69. }
  70. }
  71. }
  72. impl std::convert::From<::r2d2::Error> for UserError {
  73. fn from(error: r2d2::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  74. }
  75. // use diesel::result::{Error, DatabaseErrorKind};
  76. // use flowy_sqlite::ErrorKind;
  77. impl std::convert::From<flowy_sqlite::Error> for UserError {
  78. fn from(error: flowy_sqlite::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  79. }
  80. impl std::convert::From<flowy_net::errors::ServerError> for UserError {
  81. fn from(error: flowy_net::errors::ServerError) -> Self {
  82. let code = server_error_to_user_error(error.code);
  83. ErrorBuilder::new(code).error(error.msg).build()
  84. }
  85. }
  86. use flowy_net::errors::ErrorCode as ServerErrorCode;
  87. fn server_error_to_user_error(code: ServerErrorCode) -> ErrorCode {
  88. match code {
  89. ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
  90. ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
  91. ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
  92. _ => ErrorCode::InternalError,
  93. }
  94. }
  95. impl flowy_dispatch::Error for UserError {
  96. fn as_response(&self) -> EventResponse {
  97. let bytes: Bytes = self.clone().try_into().unwrap();
  98. ResponseBuilder::Err().data(bytes).build()
  99. }
  100. }
  101. pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, UserError>;
  102. impl flowy_infra::errors::Build<ErrorCode> for UserError {
  103. fn build(code: ErrorCode, msg: String) -> Self {
  104. let msg = if msg.is_empty() { format!("{}", code) } else { msg };
  105. UserError::new(code, &msg)
  106. }
  107. }