feat: Add Status::to_http (#376)
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::{
|
|||||||
codec::{encode_server, Codec, Streaming},
|
codec::{encode_server, Codec, Streaming},
|
||||||
interceptor::Interceptor,
|
interceptor::Interceptor,
|
||||||
server::{ClientStreamingService, ServerStreamingService, StreamingService, UnaryService},
|
server::{ClientStreamingService, ServerStreamingService, StreamingService, UnaryService},
|
||||||
Code, Request, Response, Status,
|
Code, Request, Status,
|
||||||
};
|
};
|
||||||
use futures_core::TryStream;
|
use futures_core::TryStream;
|
||||||
use futures_util::{future, stream, TryStreamExt};
|
use futures_util::{future, stream, TryStreamExt};
|
||||||
@@ -210,31 +210,15 @@ where
|
|||||||
|
|
||||||
http::Response::from_parts(parts, BoxBody::new(body))
|
http::Response::from_parts(parts, BoxBody::new(body))
|
||||||
}
|
}
|
||||||
Err(status) => Self::map_status(status),
|
Err(status) => status.to_http(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_status(status: Status) -> http::Response<BoxBody> {
|
|
||||||
let (mut parts, _body) = Response::new(()).into_http().into_parts();
|
|
||||||
|
|
||||||
parts.headers.insert(
|
|
||||||
http::header::CONTENT_TYPE,
|
|
||||||
http::header::HeaderValue::from_static("application/grpc"),
|
|
||||||
);
|
|
||||||
|
|
||||||
status.add_header(&mut parts.headers).unwrap();
|
|
||||||
|
|
||||||
http::Response::from_parts(parts, BoxBody::empty())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn intercept_request<A>(&self, req: Request<A>) -> Result<Request<A>, http::Response<BoxBody>> {
|
fn intercept_request<A>(&self, req: Request<A>) -> Result<Request<A>, http::Response<BoxBody>> {
|
||||||
if let Some(interceptor) = &self.interceptor {
|
if let Some(interceptor) = &self.interceptor {
|
||||||
match interceptor.call(req) {
|
match interceptor.call(req) {
|
||||||
Ok(req) => Ok(req),
|
Ok(req) => Ok(req),
|
||||||
Err(status) => {
|
Err(status) => Err(status.to_http()),
|
||||||
let res = Self::map_status(status);
|
|
||||||
return Err(res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(req)
|
Ok(req)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::body::BoxBody;
|
||||||
use crate::metadata::MetadataMap;
|
use crate::metadata::MetadataMap;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use http::header::{HeaderMap, HeaderValue};
|
use http::header::{HeaderMap, HeaderValue};
|
||||||
@@ -464,6 +465,20 @@ impl Status {
|
|||||||
metadata: metadata,
|
metadata: metadata,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build an `http::Response` from the given `Status`.
|
||||||
|
pub fn to_http(self) -> http::Response<BoxBody> {
|
||||||
|
let (mut parts, _body) = http::Response::new(()).into_parts();
|
||||||
|
|
||||||
|
parts.headers.insert(
|
||||||
|
http::header::CONTENT_TYPE,
|
||||||
|
http::header::HeaderValue::from_static("application/grpc"),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.add_header(&mut parts.headers).unwrap();
|
||||||
|
|
||||||
|
http::Response::from_parts(parts, BoxBody::empty())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Status {
|
impl fmt::Debug for Status {
|
||||||
|
|||||||
Reference in New Issue
Block a user