1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
extern crate serde_json;
extern crate log;

use ffi::ErrorCode;

use std::error::Error;
use std::{fmt, io};

pub trait ToErrorCode {
    fn to_error_code(&self) -> ErrorCode;
}

#[derive(Debug)]
pub enum IndyCryptoError {
    InvalidParam1(String),
    InvalidParam2(String),
    InvalidParam3(String),
    InvalidParam4(String),
    InvalidParam5(String),
    InvalidParam6(String),
    InvalidParam7(String),
    InvalidParam8(String),
    InvalidParam9(String),
    InvalidState(String),
    InvalidStructure(String),
    IOError(io::Error),
    AnoncredsRevocationAccumulatorIsFull(String),
    AnoncredsInvalidRevocationAccumulatorIndex(String),
    AnoncredsCredentialRevoked(String),
    AnoncredsProofRejected(String),
}

impl fmt::Display for IndyCryptoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            IndyCryptoError::InvalidParam1(ref description) => write!(f, "Invalid param 1: {}", description),
            IndyCryptoError::InvalidParam2(ref description) => write!(f, "Invalid param 2: {}", description),
            IndyCryptoError::InvalidParam3(ref description) => write!(f, "Invalid param 3: {}", description),
            IndyCryptoError::InvalidParam4(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidParam5(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidParam6(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidParam7(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidParam8(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidParam9(ref description) => write!(f, "Invalid param 4: {}", description),
            IndyCryptoError::InvalidState(ref description) => write!(f, "Invalid library state: {}", description),
            IndyCryptoError::InvalidStructure(ref description) => write!(f, "Invalid structure: {}", description),
            IndyCryptoError::IOError(ref err) => err.fmt(f),
            IndyCryptoError::AnoncredsRevocationAccumulatorIsFull(ref description) => write!(f, "Revocation accumulator is full: {}", description),
            IndyCryptoError::AnoncredsInvalidRevocationAccumulatorIndex(ref description) => write!(f, "Invalid revocation accumulator index: {}", description),
            IndyCryptoError::AnoncredsCredentialRevoked(ref description) => write!(f, "Credential revoked: {}", description),
            IndyCryptoError::AnoncredsProofRejected(ref description) => write!(f, "Proof rejected: {}", description),
        }
    }
}

impl Error for IndyCryptoError {
    fn description(&self) -> &str {
        match *self {
            IndyCryptoError::InvalidParam1(ref description) => description,
            IndyCryptoError::InvalidParam2(ref description) => description,
            IndyCryptoError::InvalidParam3(ref description) => description,
            IndyCryptoError::InvalidParam4(ref description) => description,
            IndyCryptoError::InvalidParam5(ref description) => description,
            IndyCryptoError::InvalidParam6(ref description) => description,
            IndyCryptoError::InvalidParam7(ref description) => description,
            IndyCryptoError::InvalidParam8(ref description) => description,
            IndyCryptoError::InvalidParam9(ref description) => description,
            IndyCryptoError::InvalidState(ref description) => description,
            IndyCryptoError::InvalidStructure(ref description) => description,
            IndyCryptoError::IOError(ref err) => err.description(),
            IndyCryptoError::AnoncredsRevocationAccumulatorIsFull(ref description) => description,
            IndyCryptoError::AnoncredsInvalidRevocationAccumulatorIndex(ref description) => description,
            IndyCryptoError::AnoncredsCredentialRevoked(ref description) => description,
            IndyCryptoError::AnoncredsProofRejected(ref description) => description,
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            IndyCryptoError::InvalidParam1(_) |
            IndyCryptoError::InvalidParam2(_) |
            IndyCryptoError::InvalidParam3(_) |
            IndyCryptoError::InvalidParam4(_) |
            IndyCryptoError::InvalidParam5(_) |
            IndyCryptoError::InvalidParam6(_) |
            IndyCryptoError::InvalidParam7(_) |
            IndyCryptoError::InvalidParam8(_) |
            IndyCryptoError::InvalidParam9(_) |
            IndyCryptoError::InvalidState(_) |
            IndyCryptoError::InvalidStructure(_) => None,
            IndyCryptoError::IOError(ref err) => Some(err),
            IndyCryptoError::AnoncredsRevocationAccumulatorIsFull(_) => None,
            IndyCryptoError::AnoncredsInvalidRevocationAccumulatorIndex(_) => None,
            IndyCryptoError::AnoncredsCredentialRevoked(_) => None,
            IndyCryptoError::AnoncredsProofRejected(_) => None,
        }
    }
}

impl ToErrorCode for IndyCryptoError {
    fn to_error_code(&self) -> ErrorCode {
        match *self {
            IndyCryptoError::InvalidParam1(_) => ErrorCode::CommonInvalidParam1,
            IndyCryptoError::InvalidParam2(_) => ErrorCode::CommonInvalidParam2,
            IndyCryptoError::InvalidParam3(_) => ErrorCode::CommonInvalidParam3,
            IndyCryptoError::InvalidParam4(_) => ErrorCode::CommonInvalidParam4,
            IndyCryptoError::InvalidParam5(_) => ErrorCode::CommonInvalidParam5,
            IndyCryptoError::InvalidParam6(_) => ErrorCode::CommonInvalidParam6,
            IndyCryptoError::InvalidParam7(_) => ErrorCode::CommonInvalidParam7,
            IndyCryptoError::InvalidParam8(_) => ErrorCode::CommonInvalidParam8,
            IndyCryptoError::InvalidParam9(_) => ErrorCode::CommonInvalidParam9,
            IndyCryptoError::InvalidState(_) => ErrorCode::CommonInvalidState,
            IndyCryptoError::InvalidStructure(_) => ErrorCode::CommonInvalidStructure,
            IndyCryptoError::IOError(_) => ErrorCode::CommonIOError,
            IndyCryptoError::AnoncredsRevocationAccumulatorIsFull(_) => ErrorCode::AnoncredsRevocationAccumulatorIsFull,
            IndyCryptoError::AnoncredsInvalidRevocationAccumulatorIndex(_) => ErrorCode::AnoncredsInvalidRevocationAccumulatorIndex,
            IndyCryptoError::AnoncredsCredentialRevoked(_) => ErrorCode::AnoncredsCredentialRevoked,
            IndyCryptoError::AnoncredsProofRejected(_) => ErrorCode::AnoncredsProofRejected,
        }
    }
}

impl From<serde_json::Error> for IndyCryptoError {
    fn from(err: serde_json::Error) -> IndyCryptoError {
        IndyCryptoError::InvalidStructure(err.to_string())
    }
}

impl From<log::SetLoggerError> for IndyCryptoError {
    fn from(err: log::SetLoggerError) -> IndyCryptoError{
        IndyCryptoError::InvalidState(err.description().to_owned())
    }
}