Rocket
01 / 02

Rocket: Error Catchers, Responder & Testing

Rocket: Error Catchers, Responder & Testing

Custom Error Catchers

#[catch(404)]
fn not_found() -> Json<ErrorResponse> {
    Json(ErrorResponse { message: "Resource not found".into() })
}

rocket::build().register("/", catchers![not_found])

Custom Responder

Implementing the Responder trait for a custom type lets that type define how it converts into an HTTP response (status, headers, body) when returned from a handler -- flexibility beyond Rocket's built-in Json type.

Testing With a Local Client

let client = Client::tracked(rocket()).unwrap();
let response = client.get("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);

// Simulates requests directly against routes/handlers --
// no real network-bound server needed for the test

Configuration Profiles

Rocket.toml (or environment variables) supports separate profiles per environment -- development, release, custom -- letting the same application code run with an appropriately different database URL or log level in each.

The Macro-Heavy Trade-off

Rocket's heavier use of procedural macros reduces boilerplate but can feel less transparent about what's happening under the hood, compared to a more explicit, function-composition-based Rust framework -- a real stylistic trade-off worth weighing.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free