1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::fmt::Display;
use std::fmt::Formatter;
use anyhow::anyhow;
use http::response::Parts;
use http::HeaderMap;
use http::HeaderValue;
use http::Response;
use http::StatusCode;
use super::IncomingAsyncBody;
use crate::Error;
use crate::ErrorKind;
use crate::Result;
pub struct ErrorResponse {
parts: Parts,
body: Vec<u8>,
}
impl ErrorResponse {
pub fn status_code(&self) -> StatusCode {
self.parts.status
}
pub fn headers(&self) -> &HeaderMap<HeaderValue> {
&self.parts.headers
}
pub fn body(&self) -> &[u8] {
&self.body
}
}
impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"status code: {:?}, headers: {:?}, body: {:?}",
self.status_code(),
self.headers(),
String::from_utf8_lossy(self.body())
)
}
}
pub async fn parse_error_response(resp: Response<IncomingAsyncBody>) -> Result<ErrorResponse> {
let (parts, body) = resp.into_parts();
let bs = body.bytes().await.map_err(|err| {
Error::new(ErrorKind::Unexpected, "reading error response")
.with_operation("http_util::parse_error_response")
.set_source(anyhow!(err))
})?;
Ok(ErrorResponse {
parts,
body: bs.to_vec(),
})
}
pub fn new_request_build_error(err: http::Error) -> Error {
Error::new(ErrorKind::Unexpected, "building http request")
.with_operation("http::Request::build")
.set_source(err)
}
pub fn new_request_sign_error(err: anyhow::Error) -> Error {
Error::new(ErrorKind::Unexpected, "signing http request")
.with_operation("reqsign::Sign")
.set_source(err)
}