1

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
UnicornsOnLSD
  • 631
  • 6
  • 24
  • 1
    It looks like your question might be answered by the answers of [How can I set the HTTP status code of a (Rust) Rocket API endpoint's Template response?](https://stackoverflow.com/q/60908423/155423); [Return JSON with an HTTP status other than 200 in Rocket](https://stackoverflow.com/q/54865824/155423); . If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jan 13 '21 at 15:36
  • 1
    @Shepmaster Both of those are related, but I personally don't see them as potential outright duplicates. The first one points in the right direction, but the solution isn't really _that_ clear. The second one can partially be used, but for OPs case, it would probably just be more straightforward to return `Result<&'static str, Status>` :) – vallentin Jan 13 '21 at 15:54
  • @vallentin can either of the links be (a) edited (question or answer) to be more general (b) answered in a different way to be more general? IMO it's better to have two 75% answers in one place than two 75% answers on two different questions. – Shepmaster Jan 13 '21 at 15:56
  • @Shepmaster I agree with the sentiment. The main "issue" I see, is that the first question talks about `Template` and the second is in the context of returning JSON. OP isn't returning JSON, which means the case can be simplified to returning `Result<&'static str, Status>` – vallentin Jan 13 '21 at 15:57
  • @vallentin And returning `Result` is _that_ conceptually different? – Shepmaster Jan 13 '21 at 15:58
  • @Shepmaster It was more the reverse. That adding an additional answer to e.g. the second one, isn't "possible". As the second question wants JSON response + Status, which is why the new type was required :) – vallentin Jan 13 '21 at 15:59
  • @Shepmaster Also, JSON is only the content type of the request not the response :) – vallentin Jan 13 '21 at 16:06
  • 1
    @Shepmaster This question can be marked as duplicate, the 2nd link you posted solved my issue :) – UnicornsOnLSD Jan 13 '21 at 21:12
  • @vallentin ^ thoughts? – Shepmaster Jan 13 '21 at 21:12
  • @Shepmaster OP has more context than us, so if OP says so, then sure. Though in the question itself OP isn't responding with JSON so people can be mislead by the duplicate :) – vallentin Jan 13 '21 at 21:16

0 Answers0