From fc86563b369d0b73a79d3e8dc9a84d5ce1513303 Mon Sep 17 00:00:00 2001 From: Julian Popescu Date: Tue, 5 Jan 2021 22:00:08 +0100 Subject: [PATCH] feat(tonic): implement From for Status (#500) Implements the conversion from `std::io::Error` to `tonic::Status`. **Motivation:** The `io::Error` conversion is currently left as unimplemented. It either should be implemented or removed if it won't be implemented. **Solution:** Implements the conversion from `std::io::Error` to `tonic::Status` --- tonic/src/status.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 13ede75f7..9a8f08131 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -572,8 +572,29 @@ impl From for h2::Error { } impl From for Status { - fn from(_io: std::io::Error) -> Self { - unimplemented!() + fn from(err: std::io::Error) -> Self { + use std::io::ErrorKind; + let code = match err.kind() { + ErrorKind::BrokenPipe + | ErrorKind::WouldBlock + | ErrorKind::WriteZero + | ErrorKind::Interrupted => Code::Internal, + ErrorKind::ConnectionRefused + | ErrorKind::ConnectionReset + | ErrorKind::NotConnected + | ErrorKind::AddrInUse + | ErrorKind::AddrNotAvailable => Code::Unavailable, + ErrorKind::AlreadyExists => Code::AlreadyExists, + ErrorKind::ConnectionAborted => Code::Aborted, + ErrorKind::InvalidData => Code::DataLoss, + ErrorKind::InvalidInput => Code::InvalidArgument, + ErrorKind::NotFound => Code::NotFound, + ErrorKind::PermissionDenied => Code::PermissionDenied, + ErrorKind::TimedOut => Code::DeadlineExceeded, + ErrorKind::UnexpectedEof => Code::OutOfRange, + _ => Code::Unknown, + }; + Status::new(code, err.to_string()) } }