I am learning to use rust v1.65.0
to send a http request. Now I have a rust code block like this:
pub async fn get_queue_cv() {
let client = Client::new();
let url_path = "/cv/gen/v1/list";
let url = format!("{}{}", get_app_config("cv.cv_api_url"), url_path);
let response = client
.post(url)
.headers(construct_headers())
.body("{}")
.send()
.await;
match response {
Ok(r) => {
let r: serde_json::Value = r.json().await.unwrap();
match r.get("result") {
Some(_) => {},
None => {},
}
let result: &serde_json::Value = r.get("result").unwrap();
let first_element = result.get(0).unwrap();
let queue_record: CvGen = serde_json::from_value(first_element.clone()).unwrap();
let tpl_resp = get_template(queue_record);
tpl_resp.await;
}
Err(e) => println!("Error: {}", e),
}
}
This code works fine, but sometimes when the server return null results, the unwrap will going o panic. Now I want to handle the unwrap failed situation, I am tried to use match, but now I found using match make the code has too much nested level and maket the code ugly. what I want just simplely return if any of unwrap failed. I have tried unwrap_or_else
but I di d not know how to define the else condition. what should I do to handle this unwrap with a better way?