errors.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. use std::fmt::Debug;
  2. use anyhow::Result;
  3. use thiserror::Error;
  4. use flowy_derive::ProtoBuf;
  5. use crate::code::ErrorCode;
  6. pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
  7. #[derive(Debug, Default, Clone, ProtoBuf, Error)]
  8. #[error("{code:?}: {msg}")]
  9. pub struct FlowyError {
  10. #[pb(index = 1)]
  11. pub code: i32,
  12. #[pb(index = 2)]
  13. pub msg: String,
  14. }
  15. macro_rules! static_flowy_error {
  16. ($name:ident, $code:expr) => {
  17. #[allow(non_snake_case, missing_docs)]
  18. pub fn $name() -> FlowyError {
  19. $code.into()
  20. }
  21. };
  22. }
  23. impl FlowyError {
  24. pub fn new<T: ToString>(code: ErrorCode, msg: T) -> Self {
  25. Self {
  26. code: code.value() as i32,
  27. msg: msg.to_string(),
  28. }
  29. }
  30. pub fn context<T: Debug>(mut self, error: T) -> Self {
  31. self.msg = format!("{:?}", error);
  32. self
  33. }
  34. pub fn is_record_not_found(&self) -> bool {
  35. self.code == ErrorCode::RecordNotFound.value()
  36. }
  37. static_flowy_error!(internal, ErrorCode::Internal);
  38. static_flowy_error!(record_not_found, ErrorCode::RecordNotFound);
  39. static_flowy_error!(workspace_name, ErrorCode::WorkspaceNameInvalid);
  40. static_flowy_error!(workspace_id, ErrorCode::WorkspaceIdInvalid);
  41. static_flowy_error!(color_style, ErrorCode::AppColorStyleInvalid);
  42. static_flowy_error!(workspace_desc, ErrorCode::WorkspaceDescTooLong);
  43. static_flowy_error!(app_name, ErrorCode::AppNameInvalid);
  44. static_flowy_error!(invalid_app_id, ErrorCode::AppIdInvalid);
  45. static_flowy_error!(view_name, ErrorCode::ViewNameInvalid);
  46. static_flowy_error!(view_thumbnail, ErrorCode::ViewThumbnailInvalid);
  47. static_flowy_error!(invalid_view_id, ErrorCode::ViewIdIsInvalid);
  48. static_flowy_error!(view_desc, ErrorCode::ViewDescTooLong);
  49. static_flowy_error!(view_data, ErrorCode::ViewDataInvalid);
  50. static_flowy_error!(unauthorized, ErrorCode::UserUnauthorized);
  51. static_flowy_error!(connection, ErrorCode::HttpServerConnectError);
  52. static_flowy_error!(email_empty, ErrorCode::EmailIsEmpty);
  53. static_flowy_error!(email_format, ErrorCode::EmailFormatInvalid);
  54. static_flowy_error!(email_exist, ErrorCode::EmailAlreadyExists);
  55. static_flowy_error!(password_empty, ErrorCode::PasswordIsEmpty);
  56. static_flowy_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
  57. static_flowy_error!(
  58. password_forbid_char,
  59. ErrorCode::PasswordContainsForbidCharacters
  60. );
  61. static_flowy_error!(password_format, ErrorCode::PasswordFormatInvalid);
  62. static_flowy_error!(password_not_match, ErrorCode::PasswordNotMatch);
  63. static_flowy_error!(name_too_long, ErrorCode::UserNameTooLong);
  64. static_flowy_error!(
  65. name_forbid_char,
  66. ErrorCode::UserNameContainForbiddenCharacters
  67. );
  68. static_flowy_error!(name_empty, ErrorCode::UserNameIsEmpty);
  69. static_flowy_error!(user_id, ErrorCode::UserIdInvalid);
  70. static_flowy_error!(user_not_exist, ErrorCode::UserNotExist);
  71. static_flowy_error!(text_too_long, ErrorCode::TextTooLong);
  72. static_flowy_error!(invalid_data, ErrorCode::InvalidData);
  73. static_flowy_error!(out_of_bounds, ErrorCode::OutOfBounds);
  74. static_flowy_error!(serde, ErrorCode::Serde);
  75. static_flowy_error!(field_record_not_found, ErrorCode::FieldRecordNotFound);
  76. static_flowy_error!(payload_none, ErrorCode::UnexpectedEmpty);
  77. static_flowy_error!(http, ErrorCode::HttpError);
  78. static_flowy_error!(
  79. unexpect_calendar_field_type,
  80. ErrorCode::UnexpectedCalendarFieldType
  81. );
  82. }
  83. impl std::convert::From<ErrorCode> for FlowyError {
  84. fn from(code: ErrorCode) -> Self {
  85. FlowyError {
  86. code: code.value() as i32,
  87. msg: format!("{}", code),
  88. }
  89. }
  90. }
  91. pub fn internal_error<T>(e: T) -> FlowyError
  92. where
  93. T: std::fmt::Debug,
  94. {
  95. FlowyError::internal().context(e)
  96. }
  97. impl std::convert::From<std::io::Error> for FlowyError {
  98. fn from(error: std::io::Error) -> Self {
  99. FlowyError::internal().context(error)
  100. }
  101. }
  102. impl std::convert::From<protobuf::ProtobufError> for FlowyError {
  103. fn from(e: protobuf::ProtobufError) -> Self {
  104. FlowyError::internal().context(e)
  105. }
  106. }
  107. impl From<anyhow::Error> for FlowyError {
  108. fn from(e: anyhow::Error) -> Self {
  109. FlowyError::internal().context(e)
  110. }
  111. }