【问题标题】:Proper Error handling for a Redis and Tonic async application with tonic::Status and redis::RedisError使用 tonic::Status 和 redis::RedisError 对 Redis 和 Tonic 异步应用程序进行正确的错误处理
【发布时间】:2022-03-20 23:43:42
【问题描述】:

我正在制作一个使用 Redis 客户端的基于 Tonic 的 gRPC 微服务。我想不出在发生异步错误时将RedisError 隐式转换为tonic::Status 的示例。

async fn make_transaction(
        &self,
        request: Request<TransactionRequest>,
    ) -> Result<Response<TransactionResponse>, Status> {

    let mut con = self.client.get_async_connection().await?;
    con.set("my_key", 42).await?;

来自 Redis 客户端的连接和设置一样可能失败。我正在使用使用 Tokio 的异步接口。我宁愿不要map 错误,因为这似乎破坏了异步。

我在想我需要实现 trait From&lt;Status&gt;From&lt;RedisError 但不知道该怎么做。这是我的尝试,但它不起作用,因为 Tonic 想要一个 tonic::Status 结构而不是我制作的 ApiError 结构:


pub struct ApiError {
}

impl From<tonic::Status> for ApiError {
    fn from(err: Status) -> ApiError {
        ApiError {  }
    }
}

impl From<RedisError> for Status {
    fn from(err: redis::RedisError) -> ApiError {
        ApiError {  }
    }
}

【问题讨论】:

    标签: rust redis tokio tonic


    【解决方案1】:

    我今天遇到了类似的问题。我设法通过使用自定义类型作为原始错误(在您的情况下为 RedisError )的转换容器来解决它,因此类似以下的内容也可能对您有用:

    pub struct ApiError {}
    
    impl From<RedisError> for ApiError {
        fn from(err: redis::RedisError) -> Self {
            Self { }
        }
    }
    
    impl From<ApiError> for Status {
      fn from(err: ApiError) -> Self {
            Self::internal("Failed talking to Redis")
        }
    }
    
    async fn set_key(client: ..., key: &str, value: i64) -> Result<(), ApiError> {
      let mut con = client.get_async_connection().await?;
      con.set(key, value).await?
    }
    
    async fn make_transaction(
            &self,
            request: Request<TransactionRequest>,
        ) -> Result<Response<TransactionResponse>, Status> {
    
      set_key(self.client, "my_key".into(), 42).await?
    }
    

    但我是 Rust 菜鸟,所以我不确定这是否是最好的方法......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2013-02-15
      • 2021-01-17
      • 2015-06-17
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      相关资源
      最近更新 更多