update example error handling
- Add error-chain dev dependency - Add error handling using `?` instead of `unwrap`
This commit is contained in:
		| @@ -22,3 +22,4 @@ libflate = "0.1.5" | |||||||
| [dev-dependencies] | [dev-dependencies] | ||||||
| env_logger = "0.4" | env_logger = "0.4" | ||||||
| serde_derive = "1.0" | serde_derive = "1.0" | ||||||
|  | error-chain = "0.10" | ||||||
|   | |||||||
| @@ -1,15 +1,24 @@ | |||||||
| // cargo run --example response_json | //! `cargo run --example response_json` | ||||||
| extern crate reqwest; | extern crate reqwest; | ||||||
|  | #[macro_use] extern crate serde_derive; | ||||||
|  | #[macro_use] extern crate error_chain; | ||||||
|  |  | ||||||
| #[macro_use] | error_chain! { | ||||||
| extern crate serde_derive; |     foreign_links { | ||||||
|  |         ReqError(reqwest::Error); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| #[derive(Debug, Deserialize)] | #[derive(Debug, Deserialize)] | ||||||
| struct Response { | struct Response { | ||||||
|     origin: String, |     origin: String, | ||||||
| } | } | ||||||
|  |  | ||||||
| fn main() { | fn run() -> Result<()> { | ||||||
|     let mut res = reqwest::get("https://httpbin.org/ip").unwrap(); |     let mut res = reqwest::get("https://httpbin.org/ip")?; | ||||||
|     println!("JSON: {:?}", res.json::<Response>()); |     let json = res.json::<Response>()?; | ||||||
|  |     println!("JSON: {:?}", json); | ||||||
|  |     Ok(()) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | quick_main!(run); | ||||||
|   | |||||||
| @@ -1,17 +1,31 @@ | |||||||
|  | //! `cargo run --example simple` | ||||||
| extern crate reqwest; | extern crate reqwest; | ||||||
| extern crate env_logger; | extern crate env_logger; | ||||||
|  | #[macro_use] extern crate error_chain; | ||||||
|  |  | ||||||
| fn main() { | error_chain! { | ||||||
|     env_logger::init().unwrap(); |     foreign_links { | ||||||
|  |         ReqError(reqwest::Error); | ||||||
|  |         IoError(std::io::Error); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | fn run() -> Result<()> { | ||||||
|  |     env_logger::init() | ||||||
|  |         .expect("Failed to initialize logger"); | ||||||
|  |  | ||||||
|     println!("GET https://www.rust-lang.org"); |     println!("GET https://www.rust-lang.org"); | ||||||
|  |  | ||||||
|     let mut res = reqwest::get("https://www.rust-lang.org").unwrap(); |     let mut res = reqwest::get("https://www.rust-lang.org")?; | ||||||
|  |  | ||||||
|     println!("Status: {}", res.status()); |     println!("Status: {}", res.status()); | ||||||
|     println!("Headers:\n{}", res.headers()); |     println!("Headers:\n{}", res.headers()); | ||||||
|  |  | ||||||
|     ::std::io::copy(&mut res, &mut ::std::io::stdout()).unwrap(); |     // copy the response body directly to stdout | ||||||
|  |     let _ = std::io::copy(&mut res, &mut std::io::stdout())?; | ||||||
|  |  | ||||||
|     println!("\n\nDone."); |     println!("\n\nDone."); | ||||||
|  |     Ok(()) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | quick_main!(run); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user