update example error handling

- Add error-chain dev dependency
- Add error handling using `?` instead of `unwrap`
This commit is contained in:
James Kominick
2017-05-21 14:34:20 -04:00
parent 241e5069b4
commit e9d5774365
3 changed files with 34 additions and 10 deletions

View File

@@ -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);