Skip to content

StatusCode trait #2314

@maxwase

Description

@maxwase

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

Proposal

Move out the method into a trait

pub trait ErrorCode {
    fn code(&self) -> tonic::Code;
}

So that we could

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 trait instead, but getting the code as i32 from std::process::ExitCode is not stable yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions