I'm struggling to properly handle errors returned from an API call via reqwest
. The code below works fine:
pub fn get_weather(cty: &Vec<String>, st: &Vec<String>) -> Result<WeatherResponse, Box<dyn std::error::Error>> {
// some stuff here
let url = reqwest::Url::parse_with_params(url, ¶ms)?;
let res: WeatherResponse = blocking::get(url)?.json()?;
Ok(res)
}
match api_call::get_weather(&input.city, &input.state) {
Ok(res) => // do some stuff,
Err(err) => println!("Error: {}", err)
}
If the API returns an error, I get something like this:
Error: error decoding response body: invalid type: string "404", expected i32 at line 1 column 12
The API though returns nice error messages like this:
{
"cod": "404",
"message": "city not found"
}
I believe what I need to do is something to this effect:
pub fn get_weather(cty: &Vec<String>, st: &Vec<String>) -> Result<WeatherResponse, Box<dyn std::error::Error>> {
// some stuff here
let url = reqwest::Url::parse_with_params(url, ¶ms)?;
let res = match blocking::get(url)? {
Ok(res) => {
let res: WeatherResponse = res.json()?;
Ok(res)
},
Err(err) => // parse to a custom error struct
};
}
But it says:
|
78 | let res = match blocking::get(url)? {
| ------------------- this expression has type `reqwest::blocking::Response`
79 | Ok(res) => {
| ^^^^^^^ expected struct `reqwest::blocking::Response`, found enum `std::result::Result`
|
= note: expected struct `reqwest::blocking::Response`
found enum `std::result::Result<_, _>`
What's the proper way to handle this?
EDIT: I think I see what the culprit is here now:
let res = match blocking::get(url)?