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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Possible errors in the library.

use cbc::cipher::block_padding::UnpadError;
use std::fmt;

#[cfg(feature = "wbindgen")]
use wasm_bindgen::JsValue;

use strum::IntoStaticStr;

use hmac::digest::MacError;

/// This crate's error type.
#[derive(Debug, IntoStaticStr)]
pub enum Error {
    /// The provided data has an invalid length. Error code: -1
    InvalidLength,
    /// The key length is invalid. Error code: -2
    InvalidKeyLength,
    /// The length of the FFI output buffer is invalid. Error code: -3
    InvalidOutputLength,
    /// The signature of the data blob does not match 0x0d0c. Error code: -11
    InvalidSignature,
    /// The MAC is invalid. Error code: -12
    InvalidMac,
    /// The operation cannot be done with this type. Error code: -13
    InvalidDataType,
    /// The data type is unknown. Error code: -21
    UnknownType,
    /// The data subtype is unknown. Error code: -22
    UnknownSubtype,
    /// The data type version is unknown. Error code: -23
    UnknownVersion,
    /// The data is invalid. Error code: -24
    InvalidData,
    /// A null pointer has been passed to the FFI interface. Error code: -31
    NullPointer,
    /// A cryptographic error occurred. Error code: -32
    CryptoError,
    /// An error with the Random Number Generator occurred. Error code: -33
    RandomError,
    /// A generic IO error has occurred. Error code: -34
    IoError(std::io::Error),
    /// There is not enough shares to regenerate a secret: -41
    NotEnoughShares,
    /// The version of the multiple data is inconsistent: -42
    InconsistentVersion,
}

impl Error {
    /// Returns the error code associated with the error.
    /// This is useful for passing the exception type across a language boundary.
    pub fn error_code(&self) -> i64 {
        match *self {
            Error::InvalidLength => -1,
            Error::InvalidKeyLength => -2,
            Error::InvalidOutputLength => -3,
            Error::InvalidSignature => -11,
            Error::InvalidMac => -12,
            Error::InvalidDataType => -13,
            Error::UnknownType => -21,
            Error::UnknownSubtype => -22,
            Error::UnknownVersion => -23,
            Error::InvalidData => -24,
            Error::NullPointer => -31,
            Error::CryptoError => -32,
            Error::RandomError => -33,
            Error::IoError(_) => -34,
            Error::NotEnoughShares => -41,
            Error::InconsistentVersion => -42,
        }
    }

    /// Returns a description of the error
    pub fn description(&self) -> String {
        match *self {
            Error::InvalidLength => "The provided data has an invalid length.".to_string(),
            Error::InvalidKeyLength => "The key length is invalid.".to_string(),
            Error::InvalidOutputLength => {
                "The length of the FFI output buffer is invalid.".to_string()
            }
            Error::InvalidSignature => {
                "The signature of the data blob does not match 0x0d0c.".to_string()
            }
            Error::InvalidMac => "The MAC is invalid.".to_string(),
            Error::InvalidDataType => "The operation cannot be done with this type.".to_string(),
            Error::UnknownType => "The data type is unknown.".to_string(),
            Error::UnknownSubtype => "The data subtype is unknown.".to_string(),
            Error::InvalidData => "The data is invalid.".to_string(),
            Error::UnknownVersion => "The data type version is unknown.".to_string(),
            Error::NullPointer => {
                "A null pointer has been passed to the FFI interface.".to_string()
            }
            Error::CryptoError => "A cryptographic error occurred.".to_string(),
            Error::RandomError => "An error with the Random Number Generator occurred.".to_string(),
            Error::IoError(ref error) => error.to_string(),
            Error::NotEnoughShares => {
                "There wasn't enough share to regenerate the secret.".to_string()
            }
            Error::InconsistentVersion => {
                "The version is not the same for all the data.".to_string()
            }
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Error::IoError(ref error) => error.fmt(f),
            _ => write!(f, "Error {}: {}", self.error_code(), self.description()),
        }
    }
}

impl From<hmac::digest::InvalidLength> for Error {
    fn from(_error: hmac::digest::InvalidLength) -> Error {
        Error::InvalidKeyLength
    }
}

impl From<MacError> for Error {
    fn from(_error: MacError) -> Error {
        Error::InvalidMac
    }
}

impl From<UnpadError> for Error {
    fn from(_error: UnpadError) -> Error {
        Error::CryptoError
    }
}

impl From<aead::Error> for Error {
    fn from(_error: aead::Error) -> Error {
        Error::InvalidMac
    }
}

impl From<rand::Error> for Error {
    fn from(_error: rand::Error) -> Error {
        Error::RandomError
    }
}

impl From<std::io::Error> for Error {
    fn from(_error: std::io::Error) -> Error {
        Error::RandomError
    }
}

impl From<argon2::Error> for Error {
    fn from(_error: argon2::Error) -> Self {
        Error::CryptoError
    }
}

#[cfg(feature = "wbindgen")]
impl From<Error> for JsValue {
    fn from(error: Error) -> JsValue {
        let js_error = js_sys::Error::new(&error.description());

        js_error.set_name(error.into());
        js_error.into()
    }
}