errors.rs 4.0 KB

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