small changes to error handling example (#250)

This commit is contained in:
Eduardo Canellas 2021-08-24 02:42:13 -03:00 committed by GitHub
parent 02e61dfdd6
commit 1b5359add7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 10 deletions

View File

@ -96,23 +96,20 @@ impl IntoResponse for AppError {
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
let (status, error_json) = match self {
let (status, error_message) = match self {
AppError::UserRepo(UserRepoError::NotFound) => {
(StatusCode::NOT_FOUND, json!("User not found"))
(StatusCode::NOT_FOUND, "User not found")
}
AppError::UserRepo(UserRepoError::InvalidUsername) => {
(StatusCode::UNPROCESSABLE_ENTITY, json!("Invalid username"))
(StatusCode::UNPROCESSABLE_ENTITY, "Invalid username")
}
};
let mut response = Json(json!({
"error": error_json,
}))
.into_response();
let body = Json(json!({
"error": error_message,
}));
*response.status_mut() = status;
response
(status, body).into_response()
}
}