errors.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use bytes::Bytes;
  2. use error_code::ErrorCode;
  3. use flowy_derive::ProtoBuf;
  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 { $code.into() }
  18. };
  19. }
  20. impl FlowyError {
  21. pub fn new(code: ErrorCode, msg: &str) -> Self {
  22. Self {
  23. code: code.value(),
  24. msg: msg.to_owned(),
  25. }
  26. }
  27. pub fn context<T: Debug>(mut self, error: T) -> Self {
  28. self.msg = format!("{:?}", error);
  29. self
  30. }
  31. static_flowy_error!(internal, ErrorCode::Internal);
  32. static_flowy_error!(record_not_found, ErrorCode::RecordNotFound);
  33. static_flowy_error!(workspace_name, ErrorCode::WorkspaceNameInvalid);
  34. static_flowy_error!(workspace_id, ErrorCode::WorkspaceIdInvalid);
  35. static_flowy_error!(color_style, ErrorCode::AppColorStyleInvalid);
  36. static_flowy_error!(workspace_desc, ErrorCode::WorkspaceDescTooLong);
  37. static_flowy_error!(app_name, ErrorCode::AppNameInvalid);
  38. static_flowy_error!(invalid_app_id, ErrorCode::AppIdInvalid);
  39. static_flowy_error!(view_name, ErrorCode::ViewNameInvalid);
  40. static_flowy_error!(view_thumbnail, ErrorCode::ViewThumbnailInvalid);
  41. static_flowy_error!(invalid_view_id, ErrorCode::ViewIdInvalid);
  42. static_flowy_error!(view_desc, ErrorCode::ViewDescTooLong);
  43. static_flowy_error!(view_data, ErrorCode::ViewDataInvalid);
  44. static_flowy_error!(unauthorized, ErrorCode::UserUnauthorized);
  45. static_flowy_error!(connection, ErrorCode::ConnectError);
  46. static_flowy_error!(email_empty, ErrorCode::EmailIsEmpty);
  47. static_flowy_error!(email_format, ErrorCode::EmailFormatInvalid);
  48. static_flowy_error!(email_exist, ErrorCode::EmailAlreadyExists);
  49. static_flowy_error!(password_empty, ErrorCode::PasswordIsEmpty);
  50. static_flowy_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
  51. static_flowy_error!(password_forbid_char, ErrorCode::PasswordContainsForbidCharacters);
  52. static_flowy_error!(password_format, ErrorCode::PasswordFormatInvalid);
  53. static_flowy_error!(password_not_match, ErrorCode::PasswordNotMatch);
  54. static_flowy_error!(name_too_long, ErrorCode::UserNameTooLong);
  55. static_flowy_error!(name_forbid_char, ErrorCode::UserNameContainForbiddenCharacters);
  56. static_flowy_error!(name_empty, ErrorCode::UserNameIsEmpty);
  57. static_flowy_error!(user_id, ErrorCode::UserIdInvalid);
  58. static_flowy_error!(user_not_exist, ErrorCode::UserNotExist);
  59. }
  60. impl std::convert::From<ErrorCode> for FlowyError {
  61. fn from(code: ErrorCode) -> Self {
  62. FlowyError {
  63. code: code.value(),
  64. msg: format!("{}", code),
  65. }
  66. }
  67. }
  68. pub fn internal_error<T>(e: T) -> FlowyError
  69. where
  70. T: std::fmt::Debug,
  71. {
  72. FlowyError::internal().context(e)
  73. }
  74. impl fmt::Display for FlowyError {
  75. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
  76. }
  77. impl lib_dispatch::Error for FlowyError {
  78. fn as_response(&self) -> EventResponse {
  79. let bytes: Bytes = self.clone().try_into().unwrap();
  80. ResponseBuilder::Err().data(bytes).build()
  81. }
  82. }
  83. impl std::convert::From<std::io::Error> for FlowyError {
  84. fn from(error: std::io::Error) -> Self { FlowyError::internal().context(error) }
  85. }
  86. impl std::convert::From<protobuf::ProtobufError> for FlowyError {
  87. fn from(e: protobuf::ProtobufError) -> Self { FlowyError::internal().context(e) }
  88. }