Skip to content

Commit 37f6263

Browse files
committed
fix: removed all .unwrap()
1 parent 82070ea commit 37f6263

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/async.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ impl<S: Sleeper> AsyncClient<S> {
7777
async fn get_response<T: Decodable>(&self, path: &str) -> Result<T, Error> {
7878
let url = format!("{}{}", self.url, path);
7979
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+
8182
if response.status_code>299 {
8283
return Err(Error::HttpResponse {
8384
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+
}
8589
});
8690
}
91+
// String::try_from(value)
8792
Ok(deserialize::<T>(&response.as_bytes())?)
8893
}
8994

@@ -120,10 +125,16 @@ impl<S: Sleeper> AsyncClient<S> {
120125
if response.status_code>299 {
121126
return Err(Error::HttpResponse {
122127
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+
},
124132
});
125133
}
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)
127138
}
128139

129140
/// Make an HTTP GET request to given URL, deserializing to `Option<T>`.
@@ -161,10 +172,16 @@ impl<S: Sleeper> AsyncClient<S> {
161172
if response.status_code>299 {
162173
return Err(Error::HttpResponse {
163174
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+
}
165179
});
166180
}
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+
};
168185
Ok(deserialize(&Vec::from_hex(&hex_str)?)?)
169186
}
170187

@@ -197,10 +214,16 @@ impl<S: Sleeper> AsyncClient<S> {
197214
if response.status_code>299 {
198215
return Err(Error::HttpResponse {
199216
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+
}
201221
});
202222
}
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+
})
204227
}
205228

206229
/// Make an HTTP GET request to given URL, deserializing to `Option<T>`.
@@ -240,7 +263,10 @@ impl<S: Sleeper> AsyncClient<S> {
240263
if response.status_code>299{
241264
return Err(Error::HttpResponse {
242265
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+
}
244270
});
245271
}
246272
Ok(())

0 commit comments

Comments
 (0)