I am making a Tonic-based gRPC microservice that uses the Redis client. I can't figure out an example of implicitly converting a RedisError
into a tonic::Status
when an asynchronous error occurs.
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?;
...
}
The connection from Redis client can fail as well as the set. I would rather not use .map_err()
since that seems to break the async.
I was thinking I need to implement the trait From<Status>
and From<RedisError>
but not sure how to do it. This is my attempt, but it doesn't work since Tonic wants a tonic::Status
, not a ApiError
struct that I made:
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 { }
}
}