Converts a `reqwest::Response` to an `io::Error`. The response should have a non-OK status.
(response: Response)
| 32 | |
| 33 | /// Converts a `reqwest::Response` to an `io::Error`. The response should have a non-OK status. |
| 34 | async fn http_response_to_io_error(response: Response) -> io::Error { |
| 35 | let status = response.status(); |
| 36 | |
| 37 | let kind = match status { |
| 38 | StatusCode::OK => panic!("Should not have been called on a successful request"), |
| 39 | |
| 40 | // Match against the codes we know the server explicitly hands us. |
| 41 | StatusCode::BAD_REQUEST => io::ErrorKind::InvalidInput, |
| 42 | StatusCode::FORBIDDEN => io::ErrorKind::PermissionDenied, |
| 43 | StatusCode::INSUFFICIENT_STORAGE => io::ErrorKind::Other, |
| 44 | StatusCode::INTERNAL_SERVER_ERROR => io::ErrorKind::Other, |
| 45 | StatusCode::NOT_FOUND => io::ErrorKind::NotFound, |
| 46 | StatusCode::PAYLOAD_TOO_LARGE => io::ErrorKind::InvalidInput, |
| 47 | StatusCode::SERVICE_UNAVAILABLE => io::ErrorKind::AddrNotAvailable, |
| 48 | StatusCode::UNAUTHORIZED => io::ErrorKind::PermissionDenied, |
| 49 | |
| 50 | _ => io::ErrorKind::Other, |
| 51 | }; |
| 52 | |
| 53 | match response.text().await { |
| 54 | Ok(text) => match serde_json::from_str::<ErrorResponse>(&text) { |
| 55 | Ok(response) => io::Error::new( |
| 56 | kind, |
| 57 | format!("{} (server code: {})", remove_control_chars(response.message), status), |
| 58 | ), |
| 59 | _ => io::Error::new( |
| 60 | kind, |
| 61 | format!( |
| 62 | "HTTP request returned status {} with text '{}'", |
| 63 | status, |
| 64 | remove_control_chars(text) |
| 65 | ), |
| 66 | ), |
| 67 | }, |
| 68 | Err(e) => io::Error::new( |
| 69 | kind, |
| 70 | format!( |
| 71 | "HTTP request returned status {} and failed to get text due to {}", |
| 72 | status, |
| 73 | remove_control_chars(e.to_string()) |
| 74 | ), |
| 75 | ), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Converts a `reqwest::Error` to an `io::Error`. |
| 80 | fn reqwest_error_to_io_error(e: reqwest::Error) -> io::Error { |
no test coverage detected