Serde errors

This commit is contained in:
Igor Katson 2022-12-08 20:58:23 +00:00
parent aacb8caaa3
commit 649d616972
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -18,12 +18,18 @@ impl ApiError {
kind: ApiErrorKind::TorrentNotFound(torrent_id), kind: ApiErrorKind::TorrentNotFound(torrent_id),
} }
} }
pub const fn dht_disabled() -> Self { pub const fn dht_disabled() -> Self {
Self { Self {
status: Some(StatusCode::NOT_FOUND), status: Some(StatusCode::NOT_FOUND),
kind: ApiErrorKind::DhtDisabled, kind: ApiErrorKind::DhtDisabled,
} }
} }
pub fn status(&self) -> StatusCode {
self.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
}
pub fn with_status(self, status: StatusCode) -> Self { pub fn with_status(self, status: StatusCode) -> Self {
Self { Self {
status: Some(status), status: Some(status),
@ -80,10 +86,7 @@ impl Serialize for ApiError {
ApiErrorKind::Other(_) => "internal_error", ApiErrorKind::Other(_) => "internal_error",
}, },
human_readable: format!("{self}"), human_readable: format!("{self}"),
status: self status: self.status().as_u16(),
.status
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
.as_u16(),
..Default::default() ..Default::default()
}; };
match &self.kind { match &self.kind {
@ -126,10 +129,7 @@ impl std::fmt::Display for ApiError {
impl IntoResponse for ApiError { impl IntoResponse for ApiError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
let mut response = axum::Json(&self).into_response(); let mut response = axum::Json(&self).into_response();
*response.status_mut() = match self.status { *response.status_mut() = self.status();
Some(s) => s,
None => StatusCode::INTERNAL_SERVER_ERROR,
};
response response
} }
} }