Skip to content

Commit

Permalink
feat(doit): don't crash if json decode fails.
Browse files Browse the repository at this point in the history
Instead, tell the delegate about it and return the error.

Fixes #33
  • Loading branch information
Byron committed Mar 20, 2015
1 parent 8bb2166 commit 0823dec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/mako/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) }\
Expand Down
12 changes: 12 additions & 0 deletions src/rust/cmn.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),

Expand Down

0 comments on commit 0823dec

Please sign in to comment.