Skip to content

Commit 0505b23

Browse files
author
puetzp
committed
Avoid panic when extracting errorType and error from API response
1 parent 18e09b8 commit 0505b23

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/client.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,14 +854,31 @@ async fn check_response(
854854
.await
855855
.map_err(Error::Reqwest)?;
856856

857-
let status = map["status"].as_str().unwrap();
857+
let status = map
858+
.get("status")
859+
.ok_or(Error::MissingField)?
860+
.as_str()
861+
.unwrap();
858862

859863
match status {
860864
"success" => Ok(map),
861-
"error" => Err(Error::ResponseError(ResponseError {
862-
kind: map["errorType"].as_str().unwrap().to_string(),
863-
message: map["error"].as_str().unwrap().to_string(),
864-
})),
865+
"error" => {
866+
let kind = map
867+
.get("errorType")
868+
.ok_or(Error::MissingField)?
869+
.as_str()
870+
.unwrap()
871+
.to_string();
872+
873+
let message = map
874+
.get("error")
875+
.ok_or(Error::MissingField)?
876+
.as_str()
877+
.unwrap()
878+
.to_string();
879+
880+
Err(Error::ResponseError(ResponseError { kind, message }))
881+
}
865882
_ => Err(Error::UnknownResponseStatus(UnknownResponseStatus(
866883
status.to_string(),
867884
))),

0 commit comments

Comments
 (0)