Skip to content

Commit 39165c6

Browse files
committed
address encoding errors
1 parent 1721c96 commit 39165c6

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

grpc/src/client/transport/tonic/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,13 @@ fn create_error_response(status: Status) -> GrpcResponse {
9595
TonicResponse::new(Box::pin(stream))
9696
}
9797

98-
fn convert_request(req: GrpcRequest) -> TonicRequest<Pin<Box<dyn Stream<Item = Bytes> + Send>>> {
98+
fn convert_request(req: GrpcRequest) -> TonicRequest<BoxStream<Bytes>> {
9999
let (metadata, extensions, stream) = req.into_parts();
100100

101-
let bytes_stream = Box::pin(stream.filter_map(|msg| {
101+
let bytes_stream = Box::pin(stream.map(|msg| {
102102
let mut buf = BytesMut::with_capacity(msg.encoded_message_size_hint().unwrap_or(0));
103-
if let Ok(()) = msg.encode(&mut buf) {
104-
Some(buf.freeze())
105-
} else {
106-
// TODO: Handle encoding failures.
107-
// If it fails, log the error and return None to filter it out.
108-
eprintln!("A message could not be downcast to Bytes and was skipped.");
109-
None
110-
}
103+
msg.encode(&mut buf).map_err(Status::internal)?;
104+
Ok(buf.freeze())
111105
}));
112106

113107
TonicRequest::from_parts(metadata, extensions, bytes_stream as _)

grpc/src/codec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tonic::{
1111
pub(crate) struct BytesCodec {}
1212

1313
impl Codec for BytesCodec {
14-
type Encode = Bytes;
14+
type Encode = Result<Bytes, Status>;
1515
type Decode = Bytes;
1616
type Encoder = BytesEncoder;
1717
type Decoder = BytesDecoder;
@@ -28,11 +28,11 @@ impl Codec for BytesCodec {
2828
pub struct BytesEncoder {}
2929

3030
impl Encoder for BytesEncoder {
31-
type Item = Bytes;
31+
type Item = Result<Bytes, Status>;
3232
type Error = Status;
3333

3434
fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
35-
dst.put_slice(&item);
35+
dst.put_slice(&item?);
3636
Ok(())
3737
}
3838
}

grpc/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ pub trait Message: Any + Send + Sync + Debug {
5858
}
5959
}
6060

61-
// Allocates messages for responses on the client side and requests on the
62-
// server.
61+
/// Allocates messages for responses on the client side and requests on the
62+
/// server.
6363
pub trait MessageAllocator: Send + Sync {
6464
fn allocate(&self) -> Box<dyn Message>;
6565
}

0 commit comments

Comments
 (0)