errors.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use crate::response::FlowyResponse;
  2. use bytes::Bytes;
  3. use serde::{Deserialize, Serialize, __private::Formatter};
  4. use serde_repr::*;
  5. use std::{fmt, fmt::Debug};
  6. pub type Result<T> = std::result::Result<T, ServerError>;
  7. use flowy_collaboration::errors::CollaborateError;
  8. #[derive(thiserror::Error, Debug, Serialize, Deserialize, Clone)]
  9. pub struct ServerError {
  10. pub code: ErrorCode,
  11. pub msg: String,
  12. }
  13. macro_rules! static_error {
  14. ($name:ident, $status:expr) => {
  15. #[allow(non_snake_case, missing_docs)]
  16. pub fn $name() -> ServerError {
  17. ServerError {
  18. code: $status,
  19. msg: format!("{}", $status),
  20. }
  21. }
  22. };
  23. }
  24. impl ServerError {
  25. static_error!(internal, ErrorCode::InternalError);
  26. static_error!(http, ErrorCode::HttpError);
  27. static_error!(payload_none, ErrorCode::PayloadUnexpectedNone);
  28. static_error!(unauthorized, ErrorCode::UserUnauthorized);
  29. static_error!(password_not_match, ErrorCode::PasswordNotMatch);
  30. static_error!(params_invalid, ErrorCode::ParamsInvalid);
  31. static_error!(connect_timeout, ErrorCode::ConnectTimeout);
  32. static_error!(connect_close, ErrorCode::ConnectClose);
  33. static_error!(connect_cancel, ErrorCode::ConnectCancel);
  34. static_error!(connect_refused, ErrorCode::ConnectRefused);
  35. static_error!(record_not_found, ErrorCode::RecordNotFound);
  36. pub fn new(msg: String, code: ErrorCode) -> Self {
  37. Self { code, msg }
  38. }
  39. pub fn context<T: Debug>(mut self, error: T) -> Self {
  40. self.msg = format!("{:?}", error);
  41. self
  42. }
  43. pub fn is_record_not_found(&self) -> bool {
  44. self.code == ErrorCode::RecordNotFound
  45. }
  46. pub fn is_unauthorized(&self) -> bool {
  47. self.code == ErrorCode::UserUnauthorized
  48. }
  49. pub fn to_collaborate_error(&self) -> CollaborateError {
  50. if self.is_record_not_found() {
  51. CollaborateError::record_not_found()
  52. } else {
  53. CollaborateError::internal().context(self.msg.clone())
  54. }
  55. }
  56. }
  57. pub fn internal_error<T>(e: T) -> ServerError
  58. where
  59. T: std::fmt::Debug,
  60. {
  61. ServerError::internal().context(e)
  62. }
  63. pub fn invalid_params<T: Debug>(e: T) -> ServerError {
  64. ServerError::params_invalid().context(e)
  65. }
  66. impl std::fmt::Display for ServerError {
  67. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  68. let msg = format!("{:?}:{}", self.code, self.msg);
  69. f.write_str(&msg)
  70. }
  71. }
  72. impl std::convert::From<&ServerError> for FlowyResponse {
  73. fn from(error: &ServerError) -> Self {
  74. FlowyResponse {
  75. data: Bytes::from(vec![]),
  76. error: Some(error.clone()),
  77. }
  78. }
  79. }
  80. #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Clone, derive_more::Display)]
  81. #[repr(u16)]
  82. pub enum ErrorCode {
  83. #[display(fmt = "Unauthorized")]
  84. UserUnauthorized = 1,
  85. #[display(fmt = "Payload too large")]
  86. PayloadOverflow = 2,
  87. #[display(fmt = "Payload deserialize failed")]
  88. PayloadSerdeFail = 3,
  89. #[display(fmt = "Unexpected empty payload")]
  90. PayloadUnexpectedNone = 4,
  91. #[display(fmt = "Params is invalid")]
  92. ParamsInvalid = 5,
  93. #[display(fmt = "Protobuf serde error")]
  94. ProtobufError = 10,
  95. #[display(fmt = "Json serde Error")]
  96. SerdeError = 11,
  97. #[display(fmt = "Email address already exists")]
  98. EmailAlreadyExists = 50,
  99. #[display(fmt = "Username and password do not match")]
  100. PasswordNotMatch = 51,
  101. #[display(fmt = "Connect refused")]
  102. ConnectRefused = 100,
  103. #[display(fmt = "Connection timeout")]
  104. ConnectTimeout = 101,
  105. #[display(fmt = "Connection closed")]
  106. ConnectClose = 102,
  107. #[display(fmt = "Connection canceled")]
  108. ConnectCancel = 103,
  109. #[display(fmt = "Sql error")]
  110. SqlError = 200,
  111. #[display(fmt = "Record not found")]
  112. RecordNotFound = 201,
  113. #[display(fmt = "Http request error")]
  114. HttpError = 300,
  115. #[display(fmt = "Internal error")]
  116. InternalError = 1000,
  117. }