Skip to content

Commit

Permalink
feat: Add Display implementation for Code (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag authored Jul 6, 2020
1 parent d68dd36 commit ab1de44
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,55 @@ pub enum Code {
__NonExhaustive,
}

impl Code {
/// Get description of this `Code`.
/// ```
/// fn make_grpc_request() -> tonic::Code {
/// // ...
/// tonic::Code::Ok
/// }
/// let code = make_grpc_request();
/// println!("Operation completed. Human readable description: {}", code.description());
/// ```
/// If you only need description in `println`, `format`, `log` and other
/// formatting contexts, you may want to use `Display` impl for `Code`
/// instead.
pub fn description(&self) -> &'static str {
match self {
Code::Ok => "The operation completed successfully",
Code::Cancelled => "The operation was cancelled",
Code::Unknown => "Unknown error",
Code::InvalidArgument => "Client specified an invalid argument",
Code::DeadlineExceeded => "Deadline expired before operation could complete",
Code::NotFound => "Some requested entity was not found",
Code::AlreadyExists => "Some entity that we attempted to create already exists",
Code::PermissionDenied => {
"The caller does not have permission to execute the specified operation"
}
Code::ResourceExhausted => "Some resource has been exhausted",
Code::FailedPrecondition => {
"The system is not in a state required for the operation's execution"
}
Code::Aborted => "The operation was aborted",
Code::OutOfRange => "Operation was attempted past the valid range",
Code::Unimplemented => "Operation is not implemented or not supported",
Code::Internal => "Internal error",
Code::Unavailable => "The service is currently unavailable",
Code::DataLoss => "Unrecoverable data loss or corruption",
Code::Unauthenticated => "The request does not have valid authentication credentials",
Code::__NonExhaustive => {
unreachable!("__NonExhaustive variant must not be constructed")
}
}
}
}

impl std::fmt::Display for Code {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.description(), f)
}
}

// ===== impl Status =====

impl Status {
Expand Down

0 comments on commit ab1de44

Please sign in to comment.