expression

gRPC: distinguish success and status errors

A call can return a protocol-level status rather than a successful response. This enum illustrates application choices for invalid input, a missing resource and an unavailable dependency. It is not the library Status type. A failed operation may have an uncertain outcome. Expected stdout (not executed): Ok("Notebook") Err(InvalidArgument) Err(NotFound) Err(Unavailable) Common mistake: Retrying every status or assuming an unavailable response proves no write occurred.

Formal Syntax & Structure

rust syntaxrust
Result<Reply, Status>

Working Example

Example Snippetrust
fn main() {    #[derive(Debug)]    enum Status { InvalidArgument, NotFound, Unavailable }    fn lookup(id: u64, online: bool) -> Result<&'static str, Status> {        if id == 0 { return Err(Status::InvalidArgument); }        if !online { return Err(Status::Unavailable); }        if id != 7 { return Err(Status::NotFound); }        Ok("Notebook")    }    for (id, online) in [(7, true), (0, true), (8, true), (7, false)] {        println!("{:?}", lookup(id, online));    }}
Rust Reference