errors.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use derive_more::Display;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
  4. use std::convert::TryInto;
  5. #[derive(Debug, Default, Clone, ProtoBuf)]
  6. pub struct UserError {
  7. #[pb(index = 1)]
  8. pub code: UserErrorCode,
  9. #[pb(index = 2)]
  10. pub msg: String,
  11. }
  12. impl UserError {
  13. fn new(code: UserErrorCode, msg: &str) -> Self {
  14. Self {
  15. code,
  16. msg: msg.to_owned(),
  17. }
  18. }
  19. }
  20. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  21. pub enum UserErrorCode {
  22. #[display(fmt = "Unknown")]
  23. Unknown = 0,
  24. #[display(fmt = "Database init failed")]
  25. DatabaseInitFailed = 1,
  26. #[display(fmt = "Get database write lock failed")]
  27. DatabaseWriteLocked = 2,
  28. #[display(fmt = "Get database read lock failed")]
  29. DatabaseReadLocked = 3,
  30. #[display(fmt = "Opening database is not belonging to the current user")]
  31. DatabaseUserDidNotMatch = 4,
  32. #[display(fmt = "Database internal error")]
  33. DatabaseInternalError = 5,
  34. #[display(fmt = "User not login yet")]
  35. UserNotLoginYet = 10,
  36. #[display(fmt = "Get current id read lock failed")]
  37. ReadCurrentIdFailed = 11,
  38. #[display(fmt = "Get current id write lock failed")]
  39. WriteCurrentIdFailed = 12,
  40. #[display(fmt = "Email format is not correct")]
  41. EmailInvalid = 20,
  42. #[display(fmt = "Password format is not correct")]
  43. PasswordInvalid = 21,
  44. #[display(fmt = "User name is invalid")]
  45. UserNameInvalid = 22,
  46. #[display(fmt = "User workspace is invalid")]
  47. UserWorkspaceInvalid = 23,
  48. #[display(fmt = "User id is invalid")]
  49. UserIdInvalid = 24,
  50. }
  51. impl std::default::Default for UserErrorCode {
  52. fn default() -> Self { UserErrorCode::Unknown }
  53. }
  54. impl std::convert::From<flowy_database::result::Error> for UserError {
  55. fn from(error: flowy_database::result::Error) -> Self {
  56. ErrorBuilder::new(UserErrorCode::DatabaseInternalError)
  57. .error(error)
  58. .build()
  59. }
  60. }
  61. impl std::convert::From<flowy_sqlite::Error> for UserError {
  62. fn from(error: flowy_sqlite::Error) -> Self {
  63. ErrorBuilder::new(UserErrorCode::DatabaseInternalError)
  64. .error(error)
  65. .build()
  66. }
  67. }
  68. impl flowy_dispatch::Error for UserError {
  69. fn as_response(&self) -> EventResponse {
  70. let bytes: Vec<u8> = self.clone().try_into().unwrap();
  71. ResponseBuilder::Err().data(bytes).build()
  72. }
  73. }
  74. pub struct ErrorBuilder {
  75. pub code: UserErrorCode,
  76. pub msg: Option<String>,
  77. }
  78. impl ErrorBuilder {
  79. pub fn new(code: UserErrorCode) -> Self { ErrorBuilder { code, msg: None } }
  80. pub fn msg<T>(mut self, msg: T) -> Self
  81. where
  82. T: Into<String>,
  83. {
  84. self.msg = Some(msg.into());
  85. self
  86. }
  87. pub fn error<T>(mut self, msg: T) -> Self
  88. where
  89. T: std::fmt::Debug,
  90. {
  91. self.msg = Some(format!("{:?}", msg));
  92. self
  93. }
  94. pub fn build(mut self) -> UserError {
  95. UserError::new(self.code, &self.msg.take().unwrap_or("".to_owned()))
  96. }
  97. }