@@ -77,13 +77,18 @@ impl<S: Sleeper> AsyncClient<S> {
77
77
async fn get_response < T : Decodable > ( & self , path : & str ) -> Result < T , Error > {
78
78
let url = format ! ( "{}{}" , self . url, path) ;
79
79
let response = self . get_with_retry ( & url) . await ?;
80
-
80
+ // let x=response.as_str().map(|s| s.to_owned()).map_err(Error::InvalidResponse).unwrap_or_else(|s| s.to_stirng());
81
+
81
82
if response. status_code >299 {
82
83
return Err ( Error :: HttpResponse {
83
84
status : response. status_code as u16 ,
84
- message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
85
+ message : match response. as_str ( ) {
86
+ Ok ( resp) => resp. to_string ( ) ,
87
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
88
+ }
85
89
} ) ;
86
90
}
91
+ // String::try_from(value)
87
92
Ok ( deserialize :: < T > ( & response. as_bytes ( ) ) ?)
88
93
}
89
94
@@ -120,10 +125,16 @@ impl<S: Sleeper> AsyncClient<S> {
120
125
if response. status_code >299 {
121
126
return Err ( Error :: HttpResponse {
122
127
status : response. status_code as u16 ,
123
- message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
128
+ message : match response. as_str ( ) {
129
+ Ok ( resp) => resp. to_string ( ) ,
130
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
131
+ } ,
124
132
} ) ;
125
133
}
126
- serde_json:: from_str ( & response. as_str ( ) . unwrap ( ) . to_string ( ) ) . map_err ( Error :: Json )
134
+ serde_json:: from_str ( match response. as_str ( ) {
135
+ Ok ( resp) => resp,
136
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
137
+ } ) . map_err ( Error :: Json )
127
138
}
128
139
129
140
/// Make an HTTP GET request to given URL, deserializing to `Option<T>`.
@@ -161,10 +172,16 @@ impl<S: Sleeper> AsyncClient<S> {
161
172
if response. status_code >299 {
162
173
return Err ( Error :: HttpResponse {
163
174
status : response. status_code as u16 ,
164
- message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
175
+ message : match response. as_str ( ) {
176
+ Ok ( resp) => resp. to_string ( ) ,
177
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
178
+ }
165
179
} ) ;
166
180
}
167
- let hex_str =response. as_str ( ) . unwrap ( ) . to_string ( ) ;
181
+ let hex_str =match response. as_str ( ) {
182
+ Ok ( resp) => resp. to_string ( ) ,
183
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
184
+ } ;
168
185
Ok ( deserialize ( & Vec :: from_hex ( & hex_str) ?) ?)
169
186
}
170
187
@@ -197,10 +214,16 @@ impl<S: Sleeper> AsyncClient<S> {
197
214
if response. status_code >299 {
198
215
return Err ( Error :: HttpResponse {
199
216
status : response. status_code as u16 ,
200
- message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
217
+ message : match response. as_str ( ) {
218
+ Ok ( resp) => resp. to_string ( ) ,
219
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
220
+ }
201
221
} ) ;
202
222
}
203
- Ok ( response. as_str ( ) . unwrap ( ) . to_string ( ) )
223
+ Ok ( match response. as_str ( ) {
224
+ Ok ( resp) => resp. to_string ( ) ,
225
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
226
+ } )
204
227
}
205
228
206
229
/// Make an HTTP GET request to given URL, deserializing to `Option<T>`.
@@ -240,7 +263,10 @@ impl<S: Sleeper> AsyncClient<S> {
240
263
if response. status_code >299 {
241
264
return Err ( Error :: HttpResponse {
242
265
status : response. status_code as u16 ,
243
- message : response. as_str ( ) . unwrap ( ) . to_string ( ) ,
266
+ message : match response. as_str ( ) {
267
+ Ok ( resp) => resp. to_string ( ) ,
268
+ Err ( _) => return Err ( Error :: InvalidResponse ) ,
269
+ }
244
270
} ) ;
245
271
}
246
272
Ok ( ( ) )
0 commit comments