errors.rs 5.5 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::{
  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: UserErrCode,
  13. #[pb(index = 2)]
  14. pub msg: String,
  15. }
  16. impl UserError {
  17. fn new(code: UserErrCode, msg: &str) -> Self {
  18. Self {
  19. code,
  20. msg: msg.to_owned(),
  21. }
  22. }
  23. }
  24. #[derive(Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  25. pub enum UserErrCode {
  26. #[display(fmt = "Unknown")]
  27. Unknown = 0,
  28. #[display(fmt = "Database init failed")]
  29. UserDatabaseInitFailed = 1,
  30. #[display(fmt = "Get database write lock failed")]
  31. UserDatabaseWriteLocked = 2,
  32. #[display(fmt = "Get database read lock failed")]
  33. UserDatabaseReadLocked = 3,
  34. #[display(fmt = "Opening database is not belonging to the current user")]
  35. UserDatabaseDidNotMatch = 4,
  36. #[display(fmt = "Database internal error")]
  37. UserDatabaseInternalError = 5,
  38. #[display(fmt = "Sql internal error")]
  39. SqlInternalError = 6,
  40. #[display(fmt = "User not login yet")]
  41. UserNotLoginYet = 10,
  42. #[display(fmt = "Get current id read lock failed")]
  43. ReadCurrentIdFailed = 11,
  44. #[display(fmt = "Get current id write lock failed")]
  45. WriteCurrentIdFailed = 12,
  46. #[display(fmt = "Email can not be empty or whitespace")]
  47. EmailIsEmpty = 20,
  48. #[display(fmt = "Email format is not valid")]
  49. EmailFormatInvalid = 21,
  50. #[display(fmt = "Email already exists")]
  51. EmailAlreadyExists = 22,
  52. #[display(fmt = "Password can not be empty or whitespace")]
  53. PasswordIsEmpty = 30,
  54. #[display(fmt = "Password format too long")]
  55. PasswordTooLong = 31,
  56. #[display(fmt = "Password contains forbidden characters.")]
  57. PasswordContainsForbidCharacters = 32,
  58. #[display(
  59. fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric"
  60. )]
  61. PasswordFormatInvalid = 33,
  62. #[display(fmt = "User name is too long")]
  63. UserNameTooLong = 40,
  64. #[display(fmt = "User name contain forbidden characters")]
  65. UserNameContainsForbiddenCharacters = 41,
  66. #[display(fmt = "User name can not be empty or whitespace")]
  67. UserNameIsEmpty = 42,
  68. #[display(fmt = "User workspace is invalid")]
  69. UserWorkspaceInvalid = 50,
  70. #[display(fmt = "User id is invalid")]
  71. UserIdInvalid = 51,
  72. #[display(fmt = "Create user default workspace failed")]
  73. CreateDefaultWorkspaceFailed = 52,
  74. #[display(fmt = "User default workspace already exists")]
  75. DefaultWorkspaceAlreadyExist = 53,
  76. #[display(fmt = "Server error")]
  77. ServerError = 100,
  78. }
  79. impl Debug for UserErrCode {
  80. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{}", self)) }
  81. }
  82. impl UserErrCode {
  83. pub fn to_string(&self) -> String { format!("{}", self) }
  84. }
  85. impl std::default::Default for UserErrCode {
  86. fn default() -> Self { UserErrCode::Unknown }
  87. }
  88. impl std::convert::From<flowy_database::result::Error> for UserError {
  89. fn from(error: flowy_database::result::Error) -> Self {
  90. ErrorBuilder::new(UserErrCode::UserDatabaseInternalError)
  91. .error(error)
  92. .build()
  93. }
  94. }
  95. // use diesel::result::{Error, DatabaseErrorKind};
  96. // use flowy_sqlite::ErrorKind;
  97. impl std::convert::From<flowy_sqlite::Error> for UserError {
  98. fn from(error: flowy_sqlite::Error) -> Self {
  99. // match error.kind() {
  100. // ErrorKind::Msg(_) => {},
  101. // ErrorKind::R2D2(_) => {},
  102. // ErrorKind::Migrations(_) => {},
  103. // ErrorKind::Diesel(diesel_err) => match diesel_err {
  104. // Error::InvalidCString(_) => {},
  105. // Error::DatabaseError(kind, _) => {
  106. // match kind {
  107. // DatabaseErrorKind::UniqueViolation => {
  108. //
  109. // }
  110. // _ => {}
  111. // }
  112. // },
  113. // Error::NotFound => {},
  114. // Error::QueryBuilderError(_) => {},
  115. // Error::DeserializationError(_) => {},
  116. // Error::SerializationError(_) => {},
  117. // Error::RollbackTransaction => {},
  118. // Error::AlreadyInTransaction => {},
  119. // Error::__Nonexhaustive => {},
  120. // },
  121. // ErrorKind::Connection(_) => {},
  122. // ErrorKind::Io(_) => {},
  123. // ErrorKind::UnknownMigrationExists(_) => {},
  124. // ErrorKind::__Nonexhaustive { .. } => {},
  125. // }
  126. ErrorBuilder::new(UserErrCode::SqlInternalError)
  127. .error(error)
  128. .build()
  129. }
  130. }
  131. impl std::convert::From<flowy_net::errors::ServerError> for UserError {
  132. fn from(error: flowy_net::errors::ServerError) -> Self {
  133. ErrorBuilder::new(UserErrCode::ServerError)
  134. .error(error.msg)
  135. .build()
  136. }
  137. }
  138. impl flowy_dispatch::Error for UserError {
  139. fn as_response(&self) -> EventResponse {
  140. let bytes: Bytes = self.clone().try_into().unwrap();
  141. ResponseBuilder::Err().data(bytes).build()
  142. }
  143. }
  144. pub type ErrorBuilder = flowy_infra::errors::Builder<UserErrCode, UserError>;
  145. impl flowy_infra::errors::Build<UserErrCode> for UserError {
  146. fn build(code: UserErrCode, msg: String) -> Self {
  147. let msg = if msg.is_empty() {
  148. format!("{}", code)
  149. } else {
  150. msg
  151. };
  152. UserError::new(code, &msg)
  153. }
  154. }