I try to return json body with errors in Rust's Rocket.
pub fn error_status(error: Error) -> Status {
match error {
Error::NotFound => Status::NotFound,
_ => Status::InternalServerError
}
}
#[get("/user/<id>")]
pub fn get_user(id: i32, connection: DbConn) -> Result<Json<User>, Status> {
UserService::show_user(id, &connection)
.map(|u| Json(u))
.map_err(|err| core::error_status(err))
}
When error occours it returns Status::NotFound
but with html body, I need json body.
I tried with Return JSON with an HTTP status other than 200 in Rocket
but without success. In that topic author uses JsonValue
I need Json(T)
for dynamic json body. I couldn't create Response with success :/
I could use errorCatcher but I don't want to use it in all responses, I need json only in api respnses.
How to return Errors with json body? Thank you in advance.