summaryrefslogtreecommitdiffstats
path: root/src/errors.rs
diff options
context:
space:
mode:
authorRoey Darwish Dror <roeyd@infinidat.com>2016-12-27 13:34:39 +0200
committerRoey Darwish Dror <roeyd@infinidat.com>2016-12-27 13:34:39 +0200
commit8eb4400afe7373f2884bbc0796d0eaece43c8765 (patch)
treef8a99c43052a5d839c9ef910928a4a29b1a49cc0 /src/errors.rs
parent426cb4807ee2a2683f0a97c248fd92e9fd2c09ba (diff)
Implement the standard Error trait
Implementing the standard Error trait allow composing the library error together with frameworks such as error-chain
Diffstat (limited to 'src/errors.rs')
-rw-r--r--src/errors.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs
index d18399e..b388cbb 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -1,6 +1,8 @@
//! Representations of various client errors
+use std::error::Error as ErrorTrait;
use std::io::Error as IoError;
+use std::fmt;
use hyper::Error as HttpError;
use hyper::status::StatusCode;
use rustc_serialize::json::{DecoderError, EncoderError, ParserError};
@@ -47,3 +49,34 @@ impl From<IoError> for Error {
Error::IO(error)
}
}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ try!(write!(f, "Docker Error: "));
+ match self {
+ &Error::Decoding(ref err) => return err.fmt(f),
+ &Error::Encoding(ref err) => return err.fmt(f),
+ &Error::Parse(ref err) => return err.fmt(f),
+ &Error::Http(ref err) => return err.fmt(f),
+ &Error::IO(ref err) => return err.fmt(f),
+ &Error::Fault { code, .. } => return write!(f, "{}", code),
+ };
+ }
+}
+
+impl ErrorTrait for Error {
+ fn description(&self) -> &str {
+ "Shiplift Error"
+ }
+
+ fn cause(&self) -> Option<&ErrorTrait> {
+ match self {
+ &Error::Decoding(ref err) => Some(err),
+ &Error::Encoding(ref err) => Some(err),
+ &Error::Parse(ref err) => Some(err),
+ &Error::Http(ref err) => Some(err),
+ &Error::IO(ref err) => Some(err),
+ _ => None
+ }
+ }
+}