Skip to content

Commit

Permalink
feat(tonic): implement From<io::Error> for Status (#500)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
jpopesculian authored Jan 5, 2021
1 parent 3c2a4a2 commit fc86563
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,29 @@ impl From<Status> for h2::Error {
}

impl From<std::io::Error> 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())
}
}

Expand Down

0 comments on commit fc86563

Please sign in to comment.