Skip to content

Commit

Permalink
Merge pull request #332 from fundon/json
Browse files Browse the repository at this point in the history
feat(core): If JSON Serialize failure return 500 and log Error message
  • Loading branch information
Nicholas Young authored Oct 27, 2019
2 parents 6bd69a2 + b80e2f8 commit ac1d7b0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions tide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ http-service = "0.3.1"
serde = "1.0.91"
serde_json = "1.0.39"
route-recognizer = "0.1.13"
log = "0.4.6"

[dev-dependencies]
tide = { path = "../", default-features = false }
Expand Down
52 changes: 46 additions & 6 deletions tide-core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ pub type Response = http_service::Response;

/// Serialize `t` into a JSON-encoded response.
pub fn json<T: serde::Serialize>(t: T) -> Response {
// TODO: remove the `unwrap`
http::Response::builder()
.status(http::status::StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(serde_json::to_vec(&t).unwrap()))
.unwrap()
let mut res = http::Response::builder();

match serde_json::to_vec(&t) {
Ok(v) => res
.header("Content-Type", "application/json")
.body(Body::from(v)),
Err(e) => {
log::error!("{}", e);
res.status(http::status::StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
}
}
.unwrap()
}

/// A value that is synchronously convertable into a `Response`.
Expand Down Expand Up @@ -121,24 +128,57 @@ impl<R: IntoResponse> IntoResponse for WithStatus<R> {
#[cfg(test)]
mod tests {
use super::*;
use futures::executor::block_on;

#[test]
fn test_status() {
let resp = "foo"
.with_status(http::status::StatusCode::NOT_FOUND)
.into_response();
assert_eq!(resp.status(), http::status::StatusCode::NOT_FOUND);
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
}

#[test]
fn byte_vec_content_type() {
let resp = String::from("foo").into_bytes().into_response();
assert_eq!(resp.headers()["Content-Type"], "application/octet-stream");
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
}

#[test]
fn string_content_type() {
let resp = String::from("foo").into_response();
assert_eq!(resp.headers()["Content-Type"], "text/plain; charset=utf-8");
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
}

#[test]
fn json_content_type() {
use std::collections::BTreeMap;

let mut map = BTreeMap::new();
map.insert(Some("a"), 2);
map.insert(Some("b"), 4);
map.insert(None, 6);

let resp = json(map);
assert_eq!(
resp.status(),
http::status::StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"");

let mut map = BTreeMap::new();
map.insert("a", 2);
map.insert("b", 4);
map.insert("c", 6);

let resp = json(map);
assert_eq!(resp.status(), http::status::StatusCode::OK);
assert_eq!(
block_on(resp.into_body().into_vec()).unwrap(),
br##"{"a":2,"b":4,"c":6}"##
);
}
}

0 comments on commit ac1d7b0

Please sign in to comment.