From 6639c14bd90843fe05a3e7696ec4899be84ba405 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 10 Nov 2018 10:32:25 +0100 Subject: [PATCH] use better http status codes --- src/main.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 16a61348..7305e55f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,12 +80,11 @@ fn handle_api_request<'a>( let json_str = value.to_string(); Ok(Response::builder() - .status(200) - .header("ContentType", "application/json") - .body(Body::from(json_str)) - .unwrap()) // fixme: really? + .status(StatusCode::OK) + .header("ContentType", "application/json") + .body(Body::from(json_str))?) } - Err(err) => Ok(error_response!(NOT_FOUND, err.to_string())) + Err(err) => Ok(error_response!(BAD_REQUEST, err.to_string())) } }); @@ -110,7 +109,7 @@ fn handle_request(req: Request) -> BoxFut { if comp_len >= 2 { let format = components[1]; if format != "json" { - http_error_future!(NOT_FOUND, format!("Unsupported format '{}'\n", format)) + http_error_future!(BAD_REQUEST, format!("Unsupported output format '{}'.", format)) } if let Some(info) = ROUTER.find_method(&components[2..]) { @@ -124,7 +123,7 @@ fn handle_request(req: Request) -> BoxFut { }; let api_method = match api_method_opt { Some(m) => m, - _ => http_error_future!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)), + _ => http_error_future!(NOT_FOUND, format!("No such method '{}'.", method)), }; // fixme: handle auth @@ -132,7 +131,7 @@ fn handle_request(req: Request) -> BoxFut { return handle_api_request(api_method, parts, body); } else { - http_error_future!(NOT_FOUND, format!("No such path '{}'\n", path)); + http_error_future!(NOT_FOUND, "Path not found."); } } } @@ -155,7 +154,7 @@ fn main() { handle_request(req).then(|result| -> Result, String> { match result { Ok(res) => Ok(res), - Err(err) => Ok(error_response!(NOT_FOUND, err.to_string())), + Err(err) => Ok(error_response!(BAD_REQUEST, err.to_string())), } }) })