errors.rs 3.4 KB

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