errors.rs 4.3 KB

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