Add a bad_request response type. Use it when form's aren't utf8.

This commit is contained in:
Sergio Benitez 2016-10-06 00:16:15 -07:00
parent 650d079b58
commit 639a78a8d6
2 changed files with 8 additions and 2 deletions

View File

@ -94,7 +94,7 @@ impl RouteGenerateExt for RouteParams {
let expr = quote_expr!(ecx, let expr = quote_expr!(ecx,
match ::std::str::from_utf8(_req.data.as_slice()) { match ::std::str::from_utf8(_req.data.as_slice()) {
Ok(s) => s, Ok(s) => s,
Err(_) => return ::rocket::Response::server_error() Err(_) => return ::rocket::Response::bad_request("form isn't utf8")
} }
); );

View File

@ -52,9 +52,15 @@ impl<'a> Response<'a> {
Response(Box::new(Empty::new(StatusCode::NotFound))) Response(Box::new(Empty::new(StatusCode::NotFound)))
} }
pub fn server_error() -> Response<'a> { pub fn server_error(reason: &str) -> Response<'a> {
warn_!("internal server error: {}", reason);
Response(Box::new(Empty::new(StatusCode::InternalServerError))) Response(Box::new(Empty::new(StatusCode::InternalServerError)))
} }
pub fn bad_request(reason: &str) -> Response<'a> {
warn_!("bad request from user: {}", reason);
Response(Box::new(Empty::new(StatusCode::BadRequest)))
}
} }
impl<'a> Deref for Response<'a> { impl<'a> Deref for Response<'a> {