diff --git a/Cargo.toml b/Cargo.toml index 42f9fd4..f13a6b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "medallion" -version = "2.0.1" +version = "2.0.2" authors = ["Thomas Gideon "] description = "JWT library for rust using serde, serde_json and openssl" homepage = "http://github.com/commandline/medallion" diff --git a/src/error.rs b/src/error.rs index ac6905e..b1578e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,18 +1,53 @@ use base64::Base64Error; use serde_json; +use std::error; +use std::fmt; use std::string::FromUtf8Error; use openssl::error::ErrorStack; #[derive(Debug)] pub enum Error { - Format, + Custom(String), Utf8(FromUtf8Error), Base64(Base64Error), JSON(serde_json::Error), - Custom(String), Crypto(ErrorStack), } +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::Custom(ref message) => &message, + Error::Utf8(ref err) => err.description(), + Error::Base64(ref err) => err.description(), + Error::JSON(ref err) => err.description(), + Error::Crypto(ref err) => err.description(), + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + Error::Custom(_) => None, + Error::Utf8(ref err) => Some(err), + Error::Base64(ref err) => Some(err), + Error::JSON(ref err) => Some(err), + Error::Crypto(ref err) => Some(err), + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Custom(ref message) => f.write_str(&message), + Error::Utf8(ref err) => err.fmt(f), + Error::Base64(ref err) => err.fmt(f), + Error::JSON(ref err) => err.fmt(f), + Error::Crypto(ref err) => err.fmt(f), + } + } +} + macro_rules! error_wrap { ($f: ty, $e: expr) => { impl From<$f> for Error {