grpc / grpc/grpc-rust

`StatusCode` trait

Open
#2,314 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

## Feature Request

### Crates

tonic

### Motivation

The trait can be useful when dealing with error statuses. tonic exposes the `Status::from_error` associated function, which creates a new status with a hardcoded `Code::Unknown` as its status code. With an additional boundary, it would be easy to define the error code.

P.S.
It would also be great to include error backtrace when it's present by iterating though its [sources](https://doc.rust-lang.org/stable/std/error/trait.Error.html#method.source)

### Proposal

Move out the [method](https://docs.rs/tonic/latest/tonic/struct.Status.html#method.code) into a trait
```rust
pub trait ErrorCode {
fn code(&self) -> tonic::Code;
}
```

So that we could

```rust
use tonic::Code;

impl ErrorCode for Error {
fn code(&self) -> Code {
match self {
Error::Two(_) => Code::FailedPrecondition,
_ => Code::Internal,
}
}
}
```

### Alternatives

It would be great to implement this via [std::process::Termination](https://doc.rust-lang.org/stable/std/process/trait.Termination.html) trait instead, but getting the code as `i32` from [std::process::ExitCode](https://doc.rust-lang.org/stable/std/process/struct.ExitCode.html) is not stable yet.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.