errors.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  6. #[derive(Debug, Default, Clone, ProtoBuf)]
  7. pub struct UserError {
  8. #[pb(index = 1)]
  9. pub code: UserErrCode,
  10. #[pb(index = 2)]
  11. pub msg: String,
  12. }
  13. impl UserError {
  14. fn new(code: UserErrCode, msg: &str) -> Self {
  15. Self {
  16. code,
  17. msg: msg.to_owned(),
  18. }
  19. }
  20. }
  21. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  22. pub enum UserErrCode {
  23. #[display(fmt = "Unknown")]
  24. Unknown = 0,
  25. #[display(fmt = "Database init failed")]
  26. UserDatabaseInitFailed = 1,
  27. #[display(fmt = "Get database write lock failed")]
  28. UserDatabaseWriteLocked = 2,
  29. #[display(fmt = "Get database read lock failed")]
  30. UserDatabaseReadLocked = 3,
  31. #[display(fmt = "Opening database is not belonging to the current user")]
  32. UserDatabaseDidNotMatch = 4,
  33. #[display(fmt = "Database internal error")]
  34. UserDatabaseInternalError = 5,
  35. #[display(fmt = "Sql internal error")]
  36. SqlInternalError = 6,
  37. #[display(fmt = "User not login yet")]
  38. UserNotLoginYet = 10,
  39. #[display(fmt = "Get current id read lock failed")]
  40. ReadCurrentIdFailed = 11,
  41. #[display(fmt = "Get current id write lock failed")]
  42. WriteCurrentIdFailed = 12,
  43. #[display(fmt = "Email format is not correct")]
  44. EmailInvalid = 20,
  45. #[display(fmt = "Password format is not correct")]
  46. PasswordInvalid = 21,
  47. #[display(fmt = "User name is invalid")]
  48. UserNameInvalid = 22,
  49. #[display(fmt = "User workspace is invalid")]
  50. UserWorkspaceInvalid = 23,
  51. #[display(fmt = "User id is invalid")]
  52. UserIdInvalid = 24,
  53. #[display(fmt = "Create user default workspace failed")]
  54. CreateDefaultWorkspaceFailed = 25,
  55. #[display(fmt = "User default workspace already exists")]
  56. DefaultWorkspaceAlreadyExist = 26,
  57. }
  58. impl std::default::Default for UserErrCode {
  59. fn default() -> Self { UserErrCode::Unknown }
  60. }
  61. impl std::convert::From<flowy_database::result::Error> for UserError {
  62. fn from(error: flowy_database::result::Error) -> Self {
  63. ErrorBuilder::new(UserErrCode::UserDatabaseInternalError)
  64. .error(error)
  65. .build()
  66. }
  67. }
  68. // use diesel::result::{Error, DatabaseErrorKind};
  69. // use flowy_sqlite::ErrorKind;
  70. impl std::convert::From<flowy_sqlite::Error> for UserError {
  71. fn from(error: flowy_sqlite::Error) -> Self {
  72. // match error.kind() {
  73. // ErrorKind::Msg(_) => {},
  74. // ErrorKind::R2D2(_) => {},
  75. // ErrorKind::Migrations(_) => {},
  76. // ErrorKind::Diesel(diesel_err) => match diesel_err {
  77. // Error::InvalidCString(_) => {},
  78. // Error::DatabaseError(kind, _) => {
  79. // match kind {
  80. // DatabaseErrorKind::UniqueViolation => {
  81. //
  82. // }
  83. // _ => {}
  84. // }
  85. // },
  86. // Error::NotFound => {},
  87. // Error::QueryBuilderError(_) => {},
  88. // Error::DeserializationError(_) => {},
  89. // Error::SerializationError(_) => {},
  90. // Error::RollbackTransaction => {},
  91. // Error::AlreadyInTransaction => {},
  92. // Error::__Nonexhaustive => {},
  93. // },
  94. // ErrorKind::Connection(_) => {},
  95. // ErrorKind::Io(_) => {},
  96. // ErrorKind::UnknownMigrationExists(_) => {},
  97. // ErrorKind::__Nonexhaustive { .. } => {},
  98. // }
  99. ErrorBuilder::new(UserErrCode::SqlInternalError)
  100. .error(error)
  101. .build()
  102. }
  103. }
  104. impl flowy_dispatch::Error for UserError {
  105. fn as_response(&self) -> EventResponse {
  106. let bytes: Bytes = self.clone().try_into().unwrap();
  107. ResponseBuilder::Err().data(bytes).build()
  108. }
  109. }
  110. pub struct ErrorBuilder {
  111. pub code: UserErrCode,
  112. pub msg: Option<String>,
  113. }
  114. impl ErrorBuilder {
  115. pub fn new(code: UserErrCode) -> Self { ErrorBuilder { code, msg: None } }
  116. pub fn msg<T>(mut self, msg: T) -> Self
  117. where
  118. T: Into<String>,
  119. {
  120. self.msg = Some(msg.into());
  121. self
  122. }
  123. pub fn error<T>(mut self, msg: T) -> Self
  124. where
  125. T: std::fmt::Debug,
  126. {
  127. self.msg = Some(format!("{:?}", msg));
  128. self
  129. }
  130. pub fn build(mut self) -> UserError {
  131. UserError::new(self.code, &self.msg.take().unwrap_or("".to_owned()))
  132. }
  133. }