errors.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 {
  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. }
  62. impl std::convert::From<ErrorCode> for FlowyError {
  63. fn from(code: ErrorCode) -> Self {
  64. FlowyError {
  65. code: code.value(),
  66. msg: format!("{}", code),
  67. }
  68. }
  69. }
  70. pub fn internal_error<T>(e: T) -> FlowyError
  71. where
  72. T: std::fmt::Debug,
  73. {
  74. FlowyError::internal().context(e)
  75. }
  76. impl fmt::Display for FlowyError {
  77. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  78. write!(f, "{:?}: {}", &self.code, &self.msg)
  79. }
  80. }
  81. impl lib_dispatch::Error for FlowyError {
  82. fn as_response(&self) -> EventResponse {
  83. let bytes: Bytes = self.clone().try_into().unwrap();
  84. ResponseBuilder::Err().data(bytes).build()
  85. }
  86. }
  87. impl std::convert::From<std::io::Error> for FlowyError {
  88. fn from(error: std::io::Error) -> Self {
  89. FlowyError::internal().context(error)
  90. }
  91. }
  92. impl std::convert::From<protobuf::ProtobufError> for FlowyError {
  93. fn from(e: protobuf::ProtobufError) -> Self {
  94. FlowyError::internal().context(e)
  95. }
  96. }