< 返回版块

sunnyregion 发表于 2021-07-13 11:37

直接println!("{:?}",err) 结果如下:

Status { code: Unknown, message: "request timed out" }

println!("{:?}",err.to_string()) 结果如下: "status: Unknown, message: "request timed out", details: [], metadata: MetadataMap { headers: {} }

请问怎样取到 Status?

评论区

写评论
作者 sunnyregion 2021-07-14 11:24

网上看到的答案:

This works:



#[tonic::async_trait]
impl Greeter for MyGreeter {
    async fn say_hello(
        &self,
        request: Request<HelloRequest>,
    ) -> Result<Response<HelloReply>, Status> {
        // try_thing returns Result<_, MyError> but can still use ?
        try_thing()?;

        // ...
    }
}

struct MyError {}

impl From<MyError> for Status {
    fn from(err: MyError) -> Status {
        // ...
    }
}

fn try_thing() -> Result<(), MyError> {
    // ...
}

--
👇
Mike Tang: 类似的,你查一下 docs.rs 上的文档

Mike Tang 2021-07-13 14:55

类似的,你查一下 docs.rs 上的文档

作者 sunnyregion 2021-07-13 11:48

我使用的是tonic

--
👇
Mike Tang: 你是用的 reqwest?

https://docs.rs/reqwest/0.11.4/reqwest/struct.Error.html#method.status

作者 sunnyregion 2021-07-13 11:47
176 |             println!("------------{:?}---------{:?}",err.status(),s_err);
    |                                                          ^^^^^^ method not found in `Box<dyn std::error::Error>`


--
👇
Mike Tang: err.status()? try it.

Mike Tang 2021-07-13 11:46

你是用的 reqwest?

https://docs.rs/reqwest/0.11.4/reqwest/struct.Error.html#method.status

Mike Tang 2021-07-13 11:40

err.status()? try it.

1 共 6 条评论, 1 页