Attach custom HTTP headers to the response when an error occurs. #572

This commit is contained in:
Sunli 2021-07-15 08:20:08 +08:00
parent 528a908f95
commit 7771d4e08f
4 changed files with 14 additions and 15 deletions

View File

@ -169,9 +169,9 @@ impl Responder for Response {
if let Some(cache_control) = self.0.cache_control().value() {
res.header("cache-control", cache_control);
}
for (name, value) in self.0.http_headers() {
res.header(name, value);
}
}
for (name, value) in self.0.http_headers() {
res.header(name, value);
}
futures_util::future::ok(res.body(serde_json::to_string(&self.0).unwrap()))
}

View File

@ -214,9 +214,9 @@ impl<'r> Responder<'r, 'static> for Response {
if let Some(cache_control) = self.0.cache_control().value() {
response.set_header(Header::new("cache-control", cache_control));
}
for (name, value) in self.0.http_headers() {
response.adjoin_header(Header::new(name.to_string(), value.to_string()));
}
}
for (name, value) in self.0.http_headers() {
response.adjoin_header(Header::new(name.to_string(), value.to_string()));
}
response.set_sized_body(body.len(), Cursor::new(body));

View File

@ -164,9 +164,9 @@ pub fn respond(resp: impl Into<async_graphql::BatchResponse>) -> tide::Result {
if let Some(cache_control) = resp.cache_control().value() {
response.insert_header(headers::CACHE_CONTROL, cache_control);
}
for (name, value) in resp.http_headers() {
response.append_header(name, value);
}
}
for (name, value) in resp.http_headers() {
response.append_header(name, value);
}
response.set_body(Body::from_json(&resp)?);
Ok(response)

View File

@ -85,12 +85,11 @@ impl Reply for BatchResponse {
resp.headers_mut().insert("cache-control", value);
}
}
for (name, value) in self.0.http_headers() {
if let (Ok(name), Ok(value)) =
(TryInto::<HeaderName>::try_into(name), value.try_into())
{
resp.headers_mut().append(name, value);
}
}
for (name, value) in self.0.http_headers() {
if let (Ok(name), Ok(value)) = (TryInto::<HeaderName>::try_into(name), value.try_into())
{
resp.headers_mut().append(name, value);
}
}