2023-01-30 03:11:19 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use flowy_error::ErrorCode;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct HttpResponse {
|
2023-02-13 01:29:49 +00:00
|
|
|
pub data: Bytes,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub error: Option<HttpError>,
|
2023-01-30 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct HttpError {
|
2023-02-13 01:29:49 +00:00
|
|
|
pub code: ErrorCode,
|
|
|
|
pub msg: String,
|
2023-01-30 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpError {
|
2023-05-17 01:49:39 +00:00
|
|
|
#[allow(dead_code)]
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn is_unauthorized(&self) -> bool {
|
|
|
|
self.code == ErrorCode::UserUnauthorized
|
|
|
|
}
|
2023-01-30 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for HttpError {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{:?}: {}", self.code, self.msg)
|
|
|
|
}
|
2023-01-30 03:11:19 +00:00
|
|
|
}
|