errors.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(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 Debug for ErrorCode {
  65. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{}", self)) }
  66. }
  67. impl ErrorCode {
  68. pub fn to_string(&self) -> String { format!("{}", self) }
  69. }
  70. impl std::default::Default for ErrorCode {
  71. fn default() -> Self { ErrorCode::Unknown }
  72. }
  73. impl std::convert::From<flowy_database::Error> for UserError {
  74. fn from(error: flowy_database::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  75. }
  76. impl std::convert::From<::r2d2::Error> for UserError {
  77. fn from(error: r2d2::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  78. }
  79. // use diesel::result::{Error, DatabaseErrorKind};
  80. // use flowy_sqlite::ErrorKind;
  81. impl std::convert::From<flowy_sqlite::Error> for UserError {
  82. fn from(error: flowy_sqlite::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  83. }
  84. impl std::convert::From<flowy_net::errors::ServerError> for UserError {
  85. fn from(error: flowy_net::errors::ServerError) -> Self {
  86. let code = server_error_to_user_error(error.code);
  87. ErrorBuilder::new(code).error(error.msg).build()
  88. }
  89. }
  90. use flowy_net::errors::ErrorCode as ServerErrorCode;
  91. fn server_error_to_user_error(code: ServerErrorCode) -> ErrorCode {
  92. match code {
  93. ServerErrorCode::Unauthorized => ErrorCode::UserUnauthorized,
  94. ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
  95. ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
  96. _ => ErrorCode::InternalError,
  97. }
  98. }
  99. impl flowy_dispatch::Error for UserError {
  100. fn as_response(&self) -> EventResponse {
  101. let bytes: Bytes = self.clone().try_into().unwrap();
  102. ResponseBuilder::Err().data(bytes).build()
  103. }
  104. }
  105. pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, UserError>;
  106. impl flowy_infra::errors::Build<ErrorCode> for UserError {
  107. fn build(code: ErrorCode, msg: String) -> Self {
  108. let msg = if msg.is_empty() { format!("{}", code) } else { msg };
  109. UserError::new(code, &msg)
  110. }
  111. }