response.rs 664 B

1234567891011121314151617181920212223242526272829
  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. pub fn is_unauthorized(&self) -> bool {
  18. self.code == ErrorCode::UserUnauthorized
  19. }
  20. }
  21. impl fmt::Display for HttpError {
  22. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  23. write!(f, "{:?}: {}", self.code, self.msg)
  24. }
  25. }