Giter Club home page Giter Club logo

tonic-error's Introduction

Crates Badge License: Apache 2.0

A helper trait to assist with passing error types through return tonic::Status messages.

Usage

This works with the thiserror crates, but using that is not required. If you are not using thiserror then at the moment you will need to manually implement std::fmt::Display for your type. Your error type will also need to derive serde::{Serialize, Deserialize}.

In order to use this, you will need to #[derive(TonicError)] on your error type.

#[derive(Debug, Error, TonicError, Serialize, Deserialize)]
pub enum MathsError {
    #[error("division by zero for inputs: a={0} b={1}")]
    DivByZero(i32, i32),
}

The TonicError trait provides implementations of std::convert::TryFrom for your type, and an implementation of std::convert::From for tonic::Status.

These examples are taken from the included examples.

Server Side

async fn div(&self, req: Request<DivRequest>) -> Result<Response<DivResponse>, Status> {
    let req = req.into_inner();
    if req.b == 0 {
        return Err(MathsError::DivByZero(req.a, req.b).into());
    }
    let result = req.a as f64 / req.b as f64;
    Ok(Response::new(DivResponse { result }))
}

Client Side

pub async fn div(&mut self, a: i32, b: i32) -> Result<f64, MathsError> {
    let req = Request::new(DivRequest { a, b });

    let resp = match self.client.div(req).await {
        Ok(r) => r,
        Err(e) => match e.code() {
            Code::Internal => {
                return Err(e.try_into().expect("could not convert status to error"))
            }
            _ => panic!("error making rpc call: {e}"),
        },
    };

    Ok(resp.into_inner().result)
}

Example

See the tonic-error-example subdirectory in this repo for a working client/server example.

License

This is released under the Apache 2.0 license.

tonic-error's People

Contributors

rthomas avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.