How do I manually create an instance of reqwest::Error?

This is similar to the question I asked in this post How do I manually create an instance of reqwest::Response?

In the mock client I am writing for an integration test, I will like to seed the client with response for error cases and in such situation I want an error to be returned. That is given this trait

#[async_trait]
impl HttpClient for MockClient {
    async fn send_request(&self, path: String) -> Result<Response, Error> {
        self.res_req.get(path).unwrap()
    }
}

I want the send_request to return the error which is reqwest::Error. Problem now is, it seems there is no easy way to manually construct an instance of reqwest::Error.

Looking at the documentation here reqwest::Error - Rust I could not see any straight forward way.

Any ideas?

I believe they've deliberately made it impossible to create a reqwest::Error from outside the reqwest crate so they are free to change its implementation and possible values in a backwards compatible way.

You might need to come up with a different way of testing that logic. Depending on the scope, it might be feasible to start up a local HTTP server or send requests to httpbin.org to get exactly the HTTP error you are looking for.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.