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
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum ClientError {
AccessDenied,
NoSuchAccount,
AccountExists,
NoSuchData,
DataExists,
DataTooLarge,
NoSuchEntry,
TooManyEntries,
InvalidEntryActions(BTreeMap<Vec<u8>, EntryError>),
NoSuchKey,
InvalidOwners,
InvalidSuccessor(u64),
InvalidOperation,
InvalidInvitation,
InvitationAlreadyClaimed,
LowBalance,
NetworkFull,
NetworkOther(String),
}
impl<T: Into<String>> From<T> for ClientError {
fn from(err: T) -> Self {
ClientError::NetworkOther(err.into())
}
}
impl Display for ClientError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
ClientError::AccessDenied => write!(f, "Access denied"),
ClientError::NoSuchAccount => write!(f, "Account does not exist for client"),
ClientError::AccountExists => write!(f, "Account already exists for client"),
ClientError::NoSuchData => write!(f, "Requested data not found"),
ClientError::DataExists => write!(f, "Data given already exists"),
ClientError::DataTooLarge => write!(f, "Data given is too large"),
ClientError::NoSuchEntry => write!(f, "Requested entry not found"),
ClientError::TooManyEntries => write!(f, "Exceeded a limit on a number of entries"),
ClientError::InvalidEntryActions(ref errors) => {
write!(f, "Entry actions are invalid: {:?}", errors)
}
ClientError::NoSuchKey => write!(f, "Key does not exists"),
ClientError::InvalidOwners => write!(f, "The list of owner keys is invalid"),
ClientError::InvalidOperation => write!(f, "Requested operation is not allowed"),
ClientError::InvalidInvitation => write!(f, "Invitation token not found"),
ClientError::InvitationAlreadyClaimed => {
write!(f, "Invitation token has already been used")
}
ClientError::InvalidSuccessor(_) => {
write!(f, "Data given is not a valid successor of stored data")
}
ClientError::LowBalance => write!(f, "Insufficient account balance for this operation"),
ClientError::NetworkFull => write!(f, "Network cannot store any further data"),
ClientError::NetworkOther(ref error) => write!(f, "Error on Vault network: {}", error),
}
}
}
impl Error for ClientError {
fn description(&self) -> &str {
match *self {
ClientError::AccessDenied => "Access denied",
ClientError::NoSuchAccount => "No such account",
ClientError::AccountExists => "Account exists",
ClientError::NoSuchData => "No such data",
ClientError::DataExists => "Data exists",
ClientError::DataTooLarge => "Data is too large",
ClientError::NoSuchEntry => "No such entry",
ClientError::TooManyEntries => "Too many entries",
ClientError::InvalidEntryActions(_) => "Invalid entry actions",
ClientError::NoSuchKey => "No such key",
ClientError::InvalidOwners => "Invalid owners",
ClientError::InvalidSuccessor(_) => "Invalid data successor",
ClientError::InvalidOperation => "Invalid operation",
ClientError::InvalidInvitation => "Invalid invitation token",
ClientError::InvitationAlreadyClaimed => "Invitation token already claimed",
ClientError::LowBalance => "Low account balance",
ClientError::NetworkFull => "Network full",
ClientError::NetworkOther(ref error) => error,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum EntryError {
NoSuchEntry,
EntryExists(u64),
InvalidSuccessor(u64),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conversion_from_str_literal() {
fn mutate() -> Result<(), ClientError> {
Err("Mutation")?
}
let err_get = ClientError::from("Get");
let err_mutation = mutate().unwrap_err();
match (err_get, err_mutation) {
(ClientError::NetworkOther(val0), ClientError::NetworkOther(val1)) => {
assert_eq!(&val0, "Get");
assert_eq!(&val1, "Mutation");
}
err => panic!("Unexpected conversion: {:?}", err),
}
}
}