errors.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = "Workspace name can not be empty or whitespace")]
  9. WorkspaceNameInvalid = 0,
  10. #[display(fmt = "Workspace id can not be empty or whitespace")]
  11. WorkspaceIdInvalid = 1,
  12. #[display(fmt = "Color style of the App is invalid")]
  13. AppColorStyleInvalid = 2,
  14. #[display(fmt = "Workspace desc is invalid")]
  15. WorkspaceDescTooLong = 3,
  16. #[display(fmt = "Workspace description too long")]
  17. WorkspaceNameTooLong = 4,
  18. #[display(fmt = "App id can not be empty or whitespace")]
  19. AppIdInvalid = 10,
  20. #[display(fmt = "App name can not be empty or whitespace")]
  21. AppNameInvalid = 11,
  22. #[display(fmt = "View name can not be empty or whitespace")]
  23. ViewNameInvalid = 20,
  24. #[display(fmt = "Thumbnail of the view is invalid")]
  25. ViewThumbnailInvalid = 21,
  26. #[display(fmt = "View id can not be empty or whitespace")]
  27. ViewIdInvalid = 22,
  28. #[display(fmt = "View desc too long")]
  29. ViewDescTooLong = 23,
  30. #[display(fmt = "View data is invalid")]
  31. ViewDataInvalid = 24,
  32. #[display(fmt = "View name too long")]
  33. ViewNameTooLong = 25,
  34. #[display(fmt = "User unauthorized")]
  35. UserUnauthorized = 100,
  36. #[display(fmt = "Workspace websocket error")]
  37. WsConnectError = 200,
  38. #[display(fmt = "Server error")]
  39. InternalError = 1000,
  40. #[display(fmt = "Record not found")]
  41. RecordNotFound = 1001,
  42. }
  43. impl std::default::Default for ErrorCode {
  44. fn default() -> Self { ErrorCode::InternalError }
  45. }
  46. impl ErrorCode {
  47. pub fn value(&self) -> i32 {
  48. let code: ProtoBufErrorCode = self.clone().try_into().unwrap();
  49. code.value()
  50. }
  51. pub fn from_i32(value: i32) -> Self {
  52. match ProtoBufErrorCode::from_i32(value) {
  53. None => ErrorCode::InternalError,
  54. Some(code) => ErrorCode::try_from(&code).unwrap(),
  55. }
  56. }
  57. }