errors.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use bytes::Bytes;
  2. use derive_more::Display;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
  5. use flowy_document::errors::DocError;
  6. use flowy_net::errors::ErrorCode as ServerErrorCode;
  7. use std::{convert::TryInto, fmt};
  8. #[derive(Debug, Default, Clone, ProtoBuf)]
  9. pub struct WorkspaceError {
  10. #[pb(index = 1)]
  11. pub code: ErrorCode,
  12. #[pb(index = 2)]
  13. pub msg: String,
  14. }
  15. impl WorkspaceError {
  16. pub fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
  17. }
  18. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  19. pub enum ErrorCode {
  20. #[display(fmt = "Unknown")]
  21. Unknown = 0,
  22. #[display(fmt = "Workspace name is invalid")]
  23. WorkspaceNameInvalid = 1,
  24. #[display(fmt = "Workspace id is invalid")]
  25. WorkspaceIdInvalid = 2,
  26. #[display(fmt = "Color style of the App is invalid")]
  27. AppColorStyleInvalid = 3,
  28. #[display(fmt = "Workspace desc is invalid")]
  29. WorkspaceDescInvalid = 4,
  30. #[display(fmt = "Current workspace not found")]
  31. CurrentWorkspaceNotFound = 5,
  32. #[display(fmt = "Id of the App is invalid")]
  33. AppIdInvalid = 10,
  34. #[display(fmt = "Name of the App is invalid")]
  35. AppNameInvalid = 11,
  36. #[display(fmt = "Name of the View is invalid")]
  37. ViewNameInvalid = 20,
  38. #[display(fmt = "Thumbnail of the view is invalid")]
  39. ViewThumbnailInvalid = 21,
  40. #[display(fmt = "Id of the View is invalid")]
  41. ViewIdInvalid = 22,
  42. #[display(fmt = "Description of the View is invalid")]
  43. ViewDescInvalid = 23,
  44. #[display(fmt = "View data is invalid")]
  45. ViewDataInvalid = 24,
  46. #[display(fmt = "UserIn is empty")]
  47. UserIdIsEmpty = 100,
  48. #[display(fmt = "User unauthorized")]
  49. UserUnauthorized = 101,
  50. #[display(fmt = "Server error")]
  51. InternalError = 1000,
  52. #[display(fmt = "Record not found")]
  53. RecordNotFound = 1001,
  54. }
  55. impl std::default::Default for ErrorCode {
  56. fn default() -> Self { ErrorCode::Unknown }
  57. }
  58. impl std::convert::From<flowy_document::errors::DocError> for WorkspaceError {
  59. fn from(error: DocError) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  60. }
  61. impl std::convert::From<flowy_net::errors::ServerError> for WorkspaceError {
  62. fn from(error: flowy_net::errors::ServerError) -> Self {
  63. let code = server_error_to_workspace_error(error.code);
  64. ErrorBuilder::new(code).error(error.msg).build()
  65. }
  66. }
  67. impl std::convert::From<flowy_database::Error> for WorkspaceError {
  68. fn from(error: flowy_database::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  69. }
  70. impl flowy_dispatch::Error for WorkspaceError {
  71. fn as_response(&self) -> EventResponse {
  72. let bytes: Bytes = self.clone().try_into().unwrap();
  73. ResponseBuilder::Err().data(bytes).build()
  74. }
  75. }
  76. impl fmt::Display for WorkspaceError {
  77. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
  78. }
  79. pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, WorkspaceError>;
  80. impl flowy_infra::errors::Build<ErrorCode> for WorkspaceError {
  81. fn build(code: ErrorCode, msg: String) -> Self {
  82. let msg = if msg.is_empty() { format!("{}", code) } else { msg };
  83. WorkspaceError::new(code, &msg)
  84. }
  85. }
  86. fn server_error_to_workspace_error(code: ServerErrorCode) -> ErrorCode {
  87. match code {
  88. ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
  89. ServerErrorCode::RecordNotFound => ErrorCode::RecordNotFound,
  90. _ => ErrorCode::InternalError,
  91. }
  92. }