MCPcopy Index your code
hub / github.com/endbasic/endbasic / http_response_to_io_error

Function http_response_to_io_error

client/src/cloud.rs:34–77  ·  view source on GitHub ↗

Converts a `reqwest::Response` to an `io::Error`. The response should have a non-OK status.

(response: Response)

Source from the content-addressed store, hash-verified

32
33/// Converts a `reqwest::Response` to an `io::Error`. The response should have a non-OK status.
34async 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`.
80fn reqwest_error_to_io_error(e: reqwest::Error) -> io::Error {

Callers 9

signupMethod · 0.85
loginMethod · 0.85
logoutMethod · 0.85
get_filesMethod · 0.85
get_fileMethod · 0.85
get_file_aclsMethod · 0.85
patch_file_contentMethod · 0.85
patch_file_aclsMethod · 0.85
delete_fileMethod · 0.85

Calls 1

textMethod · 0.45

Tested by

no test coverage detected