Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0823dec

Browse files
committed
feat(doit): don't crash if json decode fails.
Instead, tell the delegate about it and return the error. Fixes #33
1 parent 8bb2166 commit 0823dec

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/mako/lib/mbuild.mako

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ match result {
363363
Result::MissingToken => println!("Missing Token"),
364364
Result::Failure(_) => println!("General Failure (Response doesn't print)"),
365365
Result::FieldClash(clashed_field) => println!("FIELD CLASH: {:?}", clashed_field),
366+
Result::JsonDecodeError(err) => println!("Json failed to decode: {:?}", err),
366367
Result::Success(_) => println!("Success (value doesn't print)"),
367368
}
368369
% endif
@@ -744,7 +745,10 @@ if enable_resource_parsing \
744745
{
745746
let mut json_response = String::new();
746747
res.read_to_string(&mut json_response).unwrap();
747-
(res, json::from_str(&json_response).unwrap())
748+
match json::from_str(&json_response) {
749+
Ok(decoded) => (res, decoded),
750+
Err(err) => return Result::JsonDecodeError(err),
751+
}
748752
}\
749753
% if supports_download:
750754
else { (res, Default::default()) }\

src/rust/cmn.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::marker::MarkerTrait;
22
use std::io::{self, Read, Seek, Cursor, Write, SeekFrom};
3+
use std;
34

45
use mime::{Mime, TopLevel, SubLevel, Attr, Value};
56
use oauth2;
@@ -8,6 +9,8 @@ use hyper::header::{ContentType, ContentLength, Headers};
89
use hyper::http::LINE_ENDING;
910
use hyper::method::Method;
1011

12+
use serde;
13+
1114
/// Identifies the Hub. There is only one per library, this trait is supposed
1215
/// to make intended use more explicit.
1316
/// The hub allows to access all resource methods more easily.
@@ -90,6 +93,11 @@ pub trait Delegate {
9093
None
9194
}
9295

96+
/// Called whenever a server response could not be decoded from json.
97+
/// It's for informational purposes only, the caller will return with an error
98+
/// accordingly.
99+
fn response_json_decode_error(&mut self, json_encoded_value: &str) {}
100+
93101
/// Called whenever the http request returns with a non-success status code.
94102
/// This can involve authentication issues, or anything else that very much
95103
/// depends on the used API method.
@@ -134,6 +142,10 @@ pub enum Result<T = ()> {
134142
/// An additional, free form field clashed with one of the built-in optional ones
135143
FieldClash(&'static str),
136144

145+
/// Shows that we failed to decode the server response.
146+
/// This can happen if the protocol changes in conjunction with strict json decoding.
147+
JsonDecodeError(serde::json::Error),
148+
137149
/// Indicates an HTTP repsonse with a non-success status code
138150
Failure(hyper::client::Response),
139151

0 commit comments

Comments
 (0)