errors.rs 3.6 KB

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