errors.rs 3.6 KB

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