Serde errors
This commit is contained in:
parent
aacb8caaa3
commit
649d616972
1 changed files with 8 additions and 8 deletions
|
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue