Making default error catchers
An application should be able to handle an error that may occur anytime during processing. In a web application, the standardized way to return an error to a client is by using an HTTP status code. Rocket provides a way to handle returning errors to clients in the form of rocket::Catcher
.
The catcher handler works just like a route handler, with a few exceptions. Let's modify our last application to see how it works. Let's recall how we implemented the user()
function:
fn user(uuid: &str) -> Result<&User, NotFound<&str>> {
let user = USERS.get(uuid);
user.ok_or(NotFound("User not found"))
}
If we request GET /user/wrongid
, the application will return an HTTP response with code 404
, a "text/plain"
content type, and a "User not found"
body. Let's change the function back to the return Option
:
fn user(uuid: &str...