Skip to content

Commit 965b628

Browse files
committedNov 3, 2019
log on json serialize failure #332
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent b6de9cf commit 965b628

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed
 

‎src/response.rs

+47-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ pub type Response = http_service::Response;
44

55
/// Serialize `t` into a JSON-encoded response.
66
pub fn json<T: serde::Serialize>(t: T) -> Response {
7-
// TODO: remove the `unwrap`
8-
http::Response::builder()
9-
.status(http::status::StatusCode::OK)
10-
.header("Content-Type", "application/json")
11-
.body(Body::from(serde_json::to_vec(&t).unwrap()))
12-
.unwrap()
7+
let mut res = http::Response::builder();
8+
match serde_json::to_vec(&t) {
9+
Ok(v) => res
10+
.header("Content-Type", "application/json")
11+
.body(Body::from(v))
12+
.unwrap()
13+
,
14+
Err(e) => {
15+
log::error!("{}", e);
16+
res.status(http::status::StatusCode::INTERNAL_SERVER_ERROR)
17+
.body(Body::empty())
18+
.unwrap()
19+
}
20+
}
1321
}
1422

1523
/// A value that is synchronously convertable into a `Response`.
@@ -118,24 +126,57 @@ impl<R: IntoResponse> IntoResponse for WithStatus<R> {
118126
#[cfg(test)]
119127
mod tests {
120128
use super::*;
129+
use futures::executor::block_on;
121130

122131
#[test]
123132
fn test_status() {
124133
let resp = "foo"
125134
.with_status(http::status::StatusCode::NOT_FOUND)
126135
.into_response();
127136
assert_eq!(resp.status(), http::status::StatusCode::NOT_FOUND);
137+
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
128138
}
129139

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

136147
#[test]
137148
fn string_content_type() {
138149
let resp = String::from("foo").into_response();
139150
assert_eq!(resp.headers()["Content-Type"], "text/plain; charset=utf-8");
151+
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"foo");
152+
}
153+
154+
#[test]
155+
fn json_content_type() {
156+
use std::collections::BTreeMap;
157+
158+
let mut map = BTreeMap::new();
159+
map.insert(Some("a"), 2);
160+
map.insert(Some("b"), 4);
161+
map.insert(None, 6);
162+
163+
let resp = json(map);
164+
assert_eq!(
165+
resp.status(),
166+
http::status::StatusCode::INTERNAL_SERVER_ERROR
167+
);
168+
assert_eq!(block_on(resp.into_body().into_vec()).unwrap(), b"");
169+
170+
let mut map = BTreeMap::new();
171+
map.insert("a", 2);
172+
map.insert("b", 4);
173+
map.insert("c", 6);
174+
175+
let resp = json(map);
176+
assert_eq!(resp.status(), http::status::StatusCode::OK);
177+
assert_eq!(
178+
block_on(resp.into_body().into_vec()).unwrap(),
179+
br##"{"a":2,"b":4,"c":6}"##
180+
);
140181
}
141182
}

0 commit comments

Comments
 (0)
Please sign in to comment.