errors.rs 4.4 KB

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