Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some code to common util library
- Loading branch information
Showing
4 changed files
with
65 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved. | ||
|
||
use std::result::Result; | ||
|
||
use tokio::io::{Error, ErrorKind}; | ||
|
||
use proto::google::rpc::Status as GrpcStatus; | ||
use tonic::Code; | ||
|
||
pub fn result_to_status(result: Result<(), Error>) -> GrpcStatus { | ||
fn kind_to_code(kind: &ErrorKind) -> Code { | ||
match kind { | ||
ErrorKind::NotFound => Code::NotFound, | ||
ErrorKind::PermissionDenied => Code::PermissionDenied, | ||
ErrorKind::ConnectionRefused => Code::Unavailable, | ||
ErrorKind::ConnectionReset => Code::Unavailable, | ||
ErrorKind::ConnectionAborted => Code::Unavailable, | ||
ErrorKind::NotConnected => Code::Internal, | ||
ErrorKind::AddrInUse => Code::Internal, | ||
ErrorKind::AddrNotAvailable => Code::Internal, | ||
ErrorKind::BrokenPipe => Code::Internal, | ||
ErrorKind::AlreadyExists => Code::AlreadyExists, | ||
ErrorKind::WouldBlock => Code::Internal, | ||
ErrorKind::InvalidInput => Code::InvalidArgument, | ||
ErrorKind::InvalidData => Code::InvalidArgument, | ||
ErrorKind::TimedOut => Code::DeadlineExceeded, | ||
ErrorKind::WriteZero => Code::Internal, | ||
ErrorKind::Interrupted => Code::Aborted, | ||
ErrorKind::Other => Code::Internal, | ||
ErrorKind::UnexpectedEof => Code::Internal, | ||
_ => Code::Internal, | ||
} | ||
} | ||
match result { | ||
Ok(()) => GrpcStatus { | ||
code: Code::Ok as i32, | ||
message: "".to_string(), | ||
details: vec![], | ||
}, | ||
Err(error) => GrpcStatus { | ||
code: kind_to_code(&error.kind()) as i32, | ||
message: format!("Error: {:?}", error), | ||
details: vec![], | ||
}, | ||
} | ||
} |