response.rs 686 B

123456789101112131415161718192021222324252627282930
  1. use bytes::Bytes;
  2. use flowy_error::ErrorCode;
  3. use serde::{Deserialize, Serialize};
  4. use std::fmt;
  5. #[derive(Debug, Serialize, Deserialize)]
  6. pub struct HttpResponse {
  7. pub data: Bytes,
  8. #[serde(skip_serializing_if = "Option::is_none")]
  9. pub error: Option<HttpError>,
  10. }
  11. #[derive(thiserror::Error, Debug, Serialize, Deserialize, Clone)]
  12. pub struct HttpError {
  13. pub code: ErrorCode,
  14. pub msg: String,
  15. }
  16. impl HttpError {
  17. #[allow(dead_code)]
  18. pub fn is_unauthorized(&self) -> bool {
  19. self.code == ErrorCode::UserUnauthorized
  20. }
  21. }
  22. impl fmt::Display for HttpError {
  23. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  24. write!(f, "{:?}: {}", self.code, self.msg)
  25. }
  26. }