From 937c5fb031f663bcc5a608074eacbbc7308460ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ian=20L=C3=A9tourneau?= Date: Fri, 21 Jul 2017 21:46:18 -0400 Subject: [PATCH] Add a 'status::BadRequest' responder. --- lib/src/response/status.rs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/src/response/status.rs b/lib/src/response/status.rs index 1aca4be7..6c0a9ff8 100644 --- a/lib/src/response/status.rs +++ b/lib/src/response/status.rs @@ -155,6 +155,46 @@ impl Responder<'static> for Reset { } } +/// Sets the status of the response to 400 (Bad Request). +/// +/// If a responder is supplied, the remainder of the response is delegated to +/// it. If there is no responder, the body of the response will be empty. +/// +/// # Examples +/// +/// A 400 Bad Request response without a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::BadRequest::<()>(None); +/// ``` +/// +/// A 400 Bad Request response _with_ a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::BadRequest(Some("error message")); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct BadRequest(pub Option); + +/// Sets the status code of the response to 400 Bad Request. If the responder is +/// `Some`, it is used to finalize the response. +impl<'r, R: Responder<'r>> Responder<'r> for BadRequest { + fn respond_to(self, req: &Request) -> Result, Status> { + let mut build = Response::build(); + if let Some(responder) = self.0 { + build.merge(responder.respond_to(req)?); + } + + build.status(Status::BadRequest).ok() + } +} + /// Sets the status of the response to 404 (Not Found). /// /// The remainder of the response is delegated to the wrapped `Responder`.