errors.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use crate::protobuf::ErrorCode as ProtoBufErrorCode;
  2. use derive_more::Display;
  3. use flowy_derive::ProtoBuf_Enum;
  4. use protobuf::ProtobufEnum;
  5. use std::convert::{TryFrom, TryInto};
  6. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  7. pub enum ErrorCode {
  8. #[display(fmt = "Email can not be empty or whitespace")]
  9. EmailIsEmpty = 0,
  10. #[display(fmt = "Email format is not valid")]
  11. EmailFormatInvalid = 1,
  12. #[display(fmt = "Email already exists")]
  13. EmailAlreadyExists = 2,
  14. #[display(fmt = "Password can not be empty or whitespace")]
  15. PasswordIsEmpty = 10,
  16. #[display(fmt = "Password format too long")]
  17. PasswordTooLong = 11,
  18. #[display(fmt = "Password contains forbidden characters.")]
  19. PasswordContainsForbidCharacters = 12,
  20. #[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
  21. PasswordFormatInvalid = 13,
  22. #[display(fmt = "Password not match")]
  23. PasswordNotMatch = 14,
  24. #[display(fmt = "User name is too long")]
  25. UserNameTooLong = 20,
  26. #[display(fmt = "User name contain forbidden characters")]
  27. UserNameContainForbiddenCharacters = 21,
  28. #[display(fmt = "User name can not be empty or whitespace")]
  29. UserNameIsEmpty = 22,
  30. #[display(fmt = "user id is empty or whitespace")]
  31. UserIdInvalid = 23,
  32. #[display(fmt = "User token is invalid")]
  33. UserUnauthorized = 24,
  34. #[display(fmt = "User not exist")]
  35. UserNotExist = 25,
  36. #[display(fmt = "Server error")]
  37. ServerError = 99,
  38. #[display(fmt = "Internal error")]
  39. InternalError = 100,
  40. }
  41. impl ErrorCode {
  42. pub fn value(&self) -> i32 {
  43. let code: ProtoBufErrorCode = self.clone().try_into().unwrap();
  44. code.value()
  45. }
  46. pub fn from_i32(value: i32) -> Self {
  47. match ProtoBufErrorCode::from_i32(value) {
  48. None => ErrorCode::InternalError,
  49. Some(code) => ErrorCode::try_from(&code).unwrap(),
  50. }
  51. }
  52. }
  53. impl std::default::Default for ErrorCode {
  54. fn default() -> Self { ErrorCode::InternalError }
  55. }