feat(doit): don't crash if json decode fails.

Instead, tell the delegate about it and return the error.

Fixes #33
This commit is contained in:
Sebastian Thiel
2015-03-20 19:31:19 +01:00
parent 8bb2166da0
commit 0823dec75c
2 changed files with 17 additions and 1 deletions

View File

@@ -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()) }\

View File

@@ -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<T = ()> {
/// 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),