How can response::status::BadRequest
be caugth?
#2702
-
Guide suggests the a possible (recommended) way to handle errors is by returning However, this doesn't work if This example presents the issue: use rocket::{response::status::BadRequest, http::Status};
#[macro_use]
extern crate rocket;
#[catch(400)]
fn bad_request() -> &'static str {
"Bad Request! :("
}
#[get("/")]
fn index() -> Result<&'static str, BadRequest<String>> {
Err(BadRequest("Terrible Request!".to_string()))
}
#[get("/")]
fn duo() -> Result<&'static str, Status> {
Err(Status::BadRequest)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![index])
.mount("/duo", routes![duo])
.register("/", catchers![bad_request])
} Requesting
But when requesting
So, what am I doing wrong here? How can I make outcome be error and thus catch |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The docs for each type's In short: |
Beta Was this translation helpful? Give feedback.
I think there's a fundamental misunderstanding with respect to error catchers. Error catchers react to failures at the routing level, not to responses with a failing status code. A response with a failing status code is just that and only that: an HTTP response with a status code >= 400.
Error catchers catch failures at the routing level. Those failures are also marked with a status code, but there is no associated HTTP response with that status code: it merely indicates the kind of routing error that occurred and is used to route the error to the appropriate catcher. It may also provide a hint towards the kind of HTTP response that should be generated as a result of the routing failure, …