From 0823dec75cc89b8e0a87a41ab2dcd1d5a405a24e Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 20 Mar 2015 19:31:19 +0100 Subject: [PATCH] feat(doit): don't crash if json decode fails. Instead, tell the delegate about it and return the error. Fixes #33 --- src/mako/lib/mbuild.mako | 6 +++++- src/rust/cmn.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/mako/lib/mbuild.mako b/src/mako/lib/mbuild.mako index 37ccb92f26e..98b08183780 100644 --- a/src/mako/lib/mbuild.mako +++ b/src/mako/lib/mbuild.mako @@ -363,6 +363,7 @@ match result { Result::MissingToken => println!("Missing Token"), Result::Failure(_) => println!("General Failure (Response doesn't print)"), Result::FieldClash(clashed_field) => println!("FIELD CLASH: {:?}", clashed_field), + Result::JsonDecodeError(err) => println!("Json failed to decode: {:?}", err), Result::Success(_) => println!("Success (value doesn't print)"), } % endif @@ -744,7 +745,10 @@ if enable_resource_parsing \ { let mut json_response = String::new(); res.read_to_string(&mut json_response).unwrap(); - (res, json::from_str(&json_response).unwrap()) + match json::from_str(&json_response) { + Ok(decoded) => (res, decoded), + Err(err) => return Result::JsonDecodeError(err), + } }\ % if supports_download: else { (res, Default::default()) }\ diff --git a/src/rust/cmn.rs b/src/rust/cmn.rs index 0391c033ad6..713efe435ee 100644 --- a/src/rust/cmn.rs +++ b/src/rust/cmn.rs @@ -1,5 +1,6 @@ use std::marker::MarkerTrait; use std::io::{self, Read, Seek, Cursor, Write, SeekFrom}; +use std; use mime::{Mime, TopLevel, SubLevel, Attr, Value}; use oauth2; @@ -8,6 +9,8 @@ use hyper::header::{ContentType, ContentLength, Headers}; use hyper::http::LINE_ENDING; use hyper::method::Method; +use serde; + /// Identifies the Hub. There is only one per library, this trait is supposed /// to make intended use more explicit. /// The hub allows to access all resource methods more easily. @@ -90,6 +93,11 @@ pub trait Delegate { None } + /// Called whenever a server response could not be decoded from json. + /// It's for informational purposes only, the caller will return with an error + /// accordingly. + fn response_json_decode_error(&mut self, json_encoded_value: &str) {} + /// Called whenever the http request returns with a non-success status code. /// This can involve authentication issues, or anything else that very much /// depends on the used API method. @@ -134,6 +142,10 @@ pub enum Result { /// An additional, free form field clashed with one of the built-in optional ones FieldClash(&'static str), + /// Shows that we failed to decode the server response. + /// This can happen if the protocol changes in conjunction with strict json decoding. + JsonDecodeError(serde::json::Error), + /// Indicates an HTTP repsonse with a non-success status code Failure(hyper::client::Response),