update example error handling
- Add error-chain dev dependency - Add error handling using `?` instead of `unwrap`
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
// cargo run --example response_json
|
||||
//! `cargo run --example response_json`
|
||||
extern crate reqwest;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use] extern crate error_chain;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
ReqError(reqwest::Error);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Response {
|
||||
origin: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut res = reqwest::get("https://httpbin.org/ip").unwrap();
|
||||
println!("JSON: {:?}", res.json::<Response>());
|
||||
fn run() -> Result<()> {
|
||||
let mut res = reqwest::get("https://httpbin.org/ip")?;
|
||||
let json = res.json::<Response>()?;
|
||||
println!("JSON: {:?}", json);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
quick_main!(run);
|
||||
|
||||
Reference in New Issue
Block a user