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

Skip to content

Commit 7dc9972

Browse files
committed
feat(API): Display + Error traits for Error struct
* improved documentation about error handling, it's less verbose yet explains what you can do. Fixes #56
1 parent be228f1 commit 7dc9972

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

src/mako/api/lib/mbuild.mako

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,18 @@ ${'.' + action_name | indent_by(13)}(${action_args});
361361
362362
match result {
363363
Err(e) => match e {
364-
Error::HttpError(err) => println!("HTTPERROR: {:?}", err),
365-
Error::MissingAPIKey => println!("Auth: Missing API Key - used if there are no scopes"),
366-
Error::MissingToken => println!("OAuth2: Missing Token"),
367-
Error::Cancelled => println!("Operation canceled by user"),
368-
Error::UploadSizeLimitExceeded(size, max_size) => println!("Upload size too big: {} of {}", size, max_size),
369-
Error::Failure(_) => println!("General Failure (hyper::client::Response doesn't print)"),
370-
Error::FieldClash(clashed_field) => println!("You added custom parameter which is part of builder: {:?}", clashed_field),
371-
Error::JsonDecodeError(err) => println!("Couldn't understand server reply - maybe API needs update: {:?}", err),
364+
// The Error enum provides details about what exactly happened.
365+
// You can also just use its `Debug`, `Display` or `Error` traits
366+
Error::HttpError(_)
367+
|Error::MissingAPIKey
368+
|Error::MissingToken
369+
|Error::Cancelled
370+
|Error::UploadSizeLimitExceeded(_, _)
371+
|Error::Failure(_)
372+
|Error::FieldClash(_)
373+
|Error::JsonDecodeError(_) => println!("{}", e),
372374
},
373-
Ok(_) => println!("Success (value doesn't print)"),
375+
Ok(res) => println!("Success: {:?}", res),
374376
}
375377
% endif
376378
</%block>

src/mako/cli/main.rs.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
},
3737
Ok(engine) => {
3838
if let Some(err) = engine.doit() {
39-
write!(io::stderr(), "TODO: display {:?}", err).ok();
39+
write!(io::stderr(), "{}", err).ok();
4040
env::set_exit_status(1);
4141
}
4242
}

src/rust/api/cmn.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::io::{self, Read, Seek, Cursor, Write, SeekFrom};
22
use std;
33
use std::fmt::{self, Display};
44
use std::str::FromStr;
5+
use std::error;
56
use std::thread::sleep_ms;
67

78
use mime::{Mime, TopLevel, SubLevel, Attr, Value};
@@ -245,6 +246,49 @@ pub enum Error {
245246
Failure(hyper::client::Response),
246247
}
247248

249+
250+
impl Display for Error {
251+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
252+
match *self {
253+
Error::HttpError(ref err) => err.fmt(f),
254+
Error::UploadSizeLimitExceeded(ref resource_size, ref max_size) =>
255+
writeln!(f, "The media size {} exceeds the maximum allowed upload size of {}"
256+
, resource_size, max_size),
257+
Error::MissingAPIKey => {
258+
writeln!(f, "The application's API key was not found in the configuration").ok();
259+
writeln!(f, "It is used as there are no Scopes defined for this method.")
260+
},
261+
Error::MissingToken =>
262+
writeln!(f, "Didn't obtain authentication token from authenticator"),
263+
Error::Cancelled =>
264+
writeln!(f, "Operation cancelled by delegate"),
265+
Error::FieldClash(field) =>
266+
writeln!(f, "The custom parameter '{}' is already provided natively by the CallBuilder.", field),
267+
Error::JsonDecodeError(ref err) => err.fmt(f),
268+
Error::Failure(ref response) =>
269+
writeln!(f, "Http status indicates failure: {:?}", response),
270+
}
271+
}
272+
}
273+
274+
impl error::Error for Error {
275+
fn description(&self) -> &str {
276+
match *self {
277+
Error::HttpError(ref err) => err.description(),
278+
Error::JsonDecodeError(ref err) => err.description(),
279+
_ => "NO DESCRIPTION POSSIBLE - use `Display.fmt()` instead"
280+
}
281+
}
282+
283+
fn cause(&self) -> Option<&error::Error> {
284+
match *self {
285+
Error::HttpError(ref err) => err.cause(),
286+
Error::JsonDecodeError(ref err) => err.cause(),
287+
_ => None
288+
}
289+
}
290+
}
291+
248292
/// A universal result type used as return for all calls.
249293
pub type Result<T> = std::result::Result<T, Error>;
250294

0 commit comments

Comments
 (0)