I want to learn how to properly deal with errors in Rust. I read a book and this example ; Now I would like to know how I should fix errors in this function:
fn get_synch_point(&self) -> Result<pv::synch::MeasPeriods, reqwest::Error> {
let url = self.root.join("/term/pv/synch");
let url = match url {
Ok(url) => url,
Err(err) => {
return Err(Error {
kind: ::std::convert::From::from(err),
url: url.ok(),
})
}
};
Ok(reqwest::get(url)?.json()?)
}
this code is incorrect; this causes a compilation error:
error[E0451]: field 'kind' of struct 'reqwest::Error' is private
--> src/main.rs:34:42
|
34 | Err(err) => return Err(Error{kind: ::std::convert::From::from(err), url: url.ok()})
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ field 'kind' is private
error[E0451]: field 'url' of struct 'reqwest::Error' is private
--> src/main.rs:34:81
|
34 | Err(err) => return Err(Error{kind: ::std::convert::From::from(err), url: url.ok()})
| ^^^^^^^^^^^^^ field 'url' is private
What is the correct procedure in this case? For me reqwest::Error
, this is a good solution, so I would like to avoid defining my own type of error:
enum MyError {
Request(reqwest::Error),
Url(url::ParseError)
}
source
share