mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-30 06:21:07 +00:00
small changes to error handling example (#250)
This commit is contained in:
parent
02e61dfdd6
commit
1b5359add7
@ -96,23 +96,20 @@ impl IntoResponse for AppError {
|
|||||||
type BodyError = Infallible;
|
type BodyError = Infallible;
|
||||||
|
|
||||||
fn into_response(self) -> Response<Self::Body> {
|
fn into_response(self) -> Response<Self::Body> {
|
||||||
let (status, error_json) = match self {
|
let (status, error_message) = match self {
|
||||||
AppError::UserRepo(UserRepoError::NotFound) => {
|
AppError::UserRepo(UserRepoError::NotFound) => {
|
||||||
(StatusCode::NOT_FOUND, json!("User not found"))
|
(StatusCode::NOT_FOUND, "User not found")
|
||||||
}
|
}
|
||||||
AppError::UserRepo(UserRepoError::InvalidUsername) => {
|
AppError::UserRepo(UserRepoError::InvalidUsername) => {
|
||||||
(StatusCode::UNPROCESSABLE_ENTITY, json!("Invalid username"))
|
(StatusCode::UNPROCESSABLE_ENTITY, "Invalid username")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut response = Json(json!({
|
let body = Json(json!({
|
||||||
"error": error_json,
|
"error": error_message,
|
||||||
}))
|
}));
|
||||||
.into_response();
|
|
||||||
|
|
||||||
*response.status_mut() = status;
|
(status, body).into_response()
|
||||||
|
|
||||||
response
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user