I'm trying to make my Rocket backend return HTTP 400 when the user enters a password that is too short. When I try to send a request with a password that is too short, the request panics and returns 500 since I'm using the Err()
function, which definitely isn't the right way of doing things.
Here is my function:
#[post("/addUser", format = "json", data = "<new_user>")]
pub fn add_user(conn: CameraServerDbConn, new_user: Json<InsertableUser>) -> &'static str {
let new_user_decoded = new_user.into_inner();
if new_user_decoded.password.chars().count() < 8 {
return Err(Status::BadRequest).unwrap();
}
let new_user_ready = InsertableUser {
username: new_user_decoded.username,
password: hash(new_user_decoded.password, DEFAULT_COST).unwrap(),
};
insert(new_user_ready, &conn).unwrap();
return "Hello";
}
What's the right way of returning a different status code? If I just try to return the status, Rust complains that I'm returning a status instead of a str.