From cabd6e0a688399b33e2df6960f55aeb8134993c3 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 19 Aug 2018 15:02:53 +0200 Subject: Update example to handle error from channel stream. --- examples/hubzilla-channel-stream.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/examples/hubzilla-channel-stream.rs b/examples/hubzilla-channel-stream.rs index f0cab19..6820341 100644 --- a/examples/hubzilla-channel-stream.rs +++ b/examples/hubzilla-channel-stream.rs @@ -8,19 +8,31 @@ use std::env; fn main() { dotenv().ok(); - let site = env::var("SITE").unwrap(); - let user = env::var("USER").unwrap(); - let password = env::var("PASSWORD").unwrap(); + let site = env::var("SITE").expect("SITE variable expected"); + let user = env::var("USER").expect("USER variable expected"); + let password = env::var("PASSWORD").expect("PASSWORD variable expected"); let client = zotapi::client(&site, &user, &password); - match serde_json::from_str(&client.channel_stream()).unwrap() { - Value::Array(v) => { - for item in v.into_iter() { - println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); + match client.channel_stream() { + Ok(payload) => { + println!("Raw payload: {}", payload); + + match serde_json::from_str(&payload) { + Ok(Value::Array(v)) => { + for item in v.into_iter() { + println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } } }, - _ => { - println!("Expected an array, really..."); + Err(e) => { + println!("Error getting channel stream: {:?}", e); } } } -- cgit v1.2.3