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

View File

@ -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
} }
} }