errors.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_net::errors::ServerError;
  6. use std::{convert::TryInto, fmt};
  7. #[derive(Debug, Default, Clone, ProtoBuf)]
  8. pub struct DocError {
  9. #[pb(index = 1)]
  10. pub code: ErrorCode,
  11. #[pb(index = 2)]
  12. pub msg: String,
  13. }
  14. impl DocError {
  15. fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
  16. pub fn is_record_not_found(&self) -> bool { self.code == ErrorCode::DocNotfound }
  17. }
  18. #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
  19. pub enum ErrorCode {
  20. #[display(fmt = "DocIdInvalid")]
  21. DocIdInvalid = 0,
  22. #[display(fmt = "DocNotfound")]
  23. DocNotfound = 1,
  24. #[display(fmt = "UserUnauthorized")]
  25. UserUnauthorized = 999,
  26. #[display(fmt = "InternalError")]
  27. InternalError = 1000,
  28. }
  29. impl std::default::Default for ErrorCode {
  30. fn default() -> Self { ErrorCode::InternalError }
  31. }
  32. impl std::convert::From<flowy_database::Error> for DocError {
  33. fn from(error: flowy_database::Error) -> Self {
  34. match error {
  35. flowy_database::Error::NotFound => ErrorBuilder::new(ErrorCode::DocNotfound).error(error).build(),
  36. _ => ErrorBuilder::new(ErrorCode::InternalError).error(error).build(),
  37. }
  38. }
  39. }
  40. impl std::convert::From<flowy_ot::errors::OTError> for DocError {
  41. fn from(error: flowy_ot::errors::OTError) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
  42. }
  43. // impl std::convert::From<::r2d2::Error> for DocError {
  44. // fn from(error: r2d2::Error) -> Self {
  45. // ErrorBuilder::new(ErrorCode::InternalError).error(error).build() } }
  46. // impl std::convert::From<flowy_sqlite::Error> for DocError {
  47. // fn from(error: flowy_sqlite::Error) -> Self {
  48. // ErrorBuilder::new(ErrorCode::InternalError).error(error).build() } }
  49. impl std::convert::From<flowy_net::errors::ServerError> for DocError {
  50. fn from(error: ServerError) -> Self {
  51. let code = server_error_to_doc_error(error.code);
  52. ErrorBuilder::new(code).error(error.msg).build()
  53. }
  54. }
  55. use flowy_net::errors::ErrorCode as ServerErrorCode;
  56. fn server_error_to_doc_error(code: ServerErrorCode) -> ErrorCode {
  57. match code {
  58. ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
  59. ServerErrorCode::RecordNotFound => ErrorCode::DocNotfound,
  60. _ => ErrorCode::InternalError,
  61. }
  62. }
  63. impl flowy_dispatch::Error for DocError {
  64. fn as_response(&self) -> EventResponse {
  65. let bytes: Bytes = self.clone().try_into().unwrap();
  66. ResponseBuilder::Err().data(bytes).build()
  67. }
  68. }
  69. impl fmt::Display for DocError {
  70. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
  71. }
  72. pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, DocError>;
  73. impl flowy_infra::errors::Build<ErrorCode> for DocError {
  74. fn build(code: ErrorCode, msg: String) -> Self { DocError::new(code, &msg) }
  75. }