#tonic #catch #await #status #convert #error

macro tonic_catch_proc

catch any tonic await error convert into Status

2 releases

0.1.2 Jun 6, 2023
0.1.1 Jan 13, 2022

#15 in #catch

Download history 3/week @ 2023-11-27 28/week @ 2024-02-19 22/week @ 2024-02-26 20/week @ 2024-03-04 13/week @ 2024-03-11

83 downloads per month
Used in tonic_catch

AGPL-3.0-or-later

4KB
91 lines

tonic_catch

catch any tonic await error convert into Status

for issue https://github.com/hyperium/tonic/discussions/716#discussioncomment-991404

use example:

use crate::var::evt::Evt;
use async_std::channel::Sender;
use tonic_catch::{tonic_catch, Result, Error};
use log::info;
use std::net::SocketAddr;
use tonic::{transport::Server, Request, Response};
pub mod proto {
  tonic::include_proto!("proto");
}

use proto::rmw_server::{Rmw, RmwServer};
use proto::Url;

pub struct RmwSrv {
  sender: Sender<Evt>,
}

#[tonic_catch]
impl Rmw for RmwSrv {
  async fn head(&self, request: Request<Url>) -> Result<()> {
    let addr = match request.remote_addr() {
      Some(addr) => addr.to_string(),
      None => String::new(),
    };

    let url = request.into_inner();
    println!("{:?} {}/{}", addr, url.addr, url.path);

    self
      .sender
      .send(Evt::Head(url.addr.parse()?, url.path))
      .await?;

    Ok(Response::new(()))
  }
}

pub async fn run(addr: SocketAddr, sender: Sender<Evt>) -> anyhow::Result<()> {
  let rmw = RmwServer::new(RmwSrv { sender });

  info!("grpc://{}", addr);

  Server::builder()
    .accept_http1(true)
    .add_service(tonic_web::enable(rmw))
    .serve(addr)
    .await?;

  Ok(())
}

About

This project is part of the rmw.link Code Project .

关于

本项目隶属于人民网络(rmw.link) 代码计划。

人民网络

Dependencies

~340–790KB
~19K SLoC