1use serde::{Deserialize, Serialize};
11use std::{
12 collections::BTreeMap,
13 error,
14 fmt::{self, Debug, Display, Formatter},
15 result,
16};
17
18pub type Result<T> = result::Result<T, Error>;
20
21pub struct ErrorDebug<'a, T>(pub &'a Result<T>);
23
24impl<'a, T> Debug for ErrorDebug<'a, T> {
25 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
26 if let Err(error) = self.0 {
27 write!(f, "{:?}", error)
28 } else {
29 write!(f, "Success")
30 }
31 }
32}
33
34#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
36pub enum Error {
37 AccessDenied,
39 NoSuchLoginPacket,
41 LoginPacketExists,
43 NoSuchData,
45 DataExists,
47 NoSuchEntry,
49 TooManyEntries,
51 InvalidEntryActions(BTreeMap<Vec<u8>, EntryError>),
53 NoSuchKey,
55 DuplicateEntryKeys,
57 InvalidOwners,
59 InvalidSuccessor(u64),
62 InvalidOwnersSuccessor(u64),
65 OpNotCausallyReady,
67 InvalidOperation,
69 SigningKeyTypeMismatch,
71 InvalidSignature,
73 DuplicateMessageId,
75 NetworkOther(String),
78 LossOfPrecision,
80 ExcessiveValue,
83 FailedToParse(String),
85 TransferIdExists,
87 InsufficientBalance,
89 NoSuchBalance,
91 NoSuchSender,
93 NoSuchRecipient,
95 BalanceExists,
97 ExceededSize,
99 Unexpected(String),
101}
102
103impl<T: Into<String>> From<T> for Error {
104 fn from(err: T) -> Self {
105 Error::NetworkOther(err.into())
106 }
107}
108
109impl Display for Error {
110 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
111 match *self {
112 Error::AccessDenied => write!(f, "Access denied"),
113 Error::NoSuchLoginPacket => write!(f, "Login packet does not exist"),
114 Error::LoginPacketExists => write!(f, "Login packet already exists at this location"),
115 Error::NoSuchData => write!(f, "Requested data not found"),
116 Error::DataExists => write!(f, "Data given already exists"),
117 Error::NoSuchEntry => write!(f, "Requested entry not found"),
118 Error::TooManyEntries => write!(f, "Exceeded a limit on a number of entries"),
119 Error::InvalidEntryActions(ref errors) => {
120 write!(f, "Entry actions are invalid: {:?}", errors)
121 }
122 Error::NoSuchKey => write!(f, "Key does not exists"),
123 Error::DuplicateEntryKeys => write!(f, "Duplicate keys in this push"),
124 Error::InvalidOwners => write!(f, "The list of owner keys is invalid"),
125 Error::InvalidOperation => write!(f, "Requested operation is not allowed"),
126 Error::InvalidSuccessor(_) => {
127 write!(f, "Data given is not a valid successor of stored data")
128 }
129 Error::InvalidOwnersSuccessor(_) => {
130 write!(f, "Data given is not a valid successor of stored data")
131 }
132 Error::OpNotCausallyReady => write!(
133 f,
134 "Data operation depends on a different replica's state than the current"
135 ),
136 Error::SigningKeyTypeMismatch => {
137 write!(f, "Mismatch between key type and signature type")
138 }
139 Error::InvalidSignature => write!(f, "Failed signature validation"),
140 Error::NetworkOther(ref error) => write!(f, "Error on Vault network: {}", error),
141 Error::LossOfPrecision => {
142 write!(f, "Lost precision on the amount of money during parsing")
143 }
144 Error::ExcessiveValue => write!(
145 f,
146 "Overflow on amount of money (check the MAX_MONEY_VALUE const)"
147 ),
148 Error::FailedToParse(ref error) => {
149 write!(f, "Failed to parse from a string: {}", error)
150 }
151 Error::TransferIdExists => write!(f, "Transfer with a given ID already exists"),
152 Error::InsufficientBalance => write!(f, "Not enough money to complete this operation"),
153 Error::NoSuchBalance => write!(f, "Balance does not exist"),
154 Error::NoSuchSender => write!(f, "Sender does not exist"),
155 Error::NoSuchRecipient => write!(f, "Recipient does not exist"),
156 Error::BalanceExists => write!(f, "Balance already exists"),
157 Error::DuplicateMessageId => write!(f, "MessageId already exists"),
158 Error::ExceededSize => write!(f, "Size of the structure exceeds the limit"),
159 Error::Unexpected(ref error) => write!(f, "Unexpected error: {}", error),
160 }
161 }
162}
163
164impl error::Error for Error {
165 fn description(&self) -> &str {
166 match *self {
167 Error::AccessDenied => "Access denied",
168 Error::NoSuchLoginPacket => "Login packet does not exist",
169 Error::LoginPacketExists => "Login packet already exists at this location",
170 Error::NoSuchData => "No such data",
171 Error::DataExists => "Data exists",
172 Error::NoSuchEntry => "No such entry",
173 Error::TooManyEntries => "Too many entries",
174 Error::InvalidEntryActions(_) => "Invalid entry actions",
175 Error::NoSuchKey => "No such key",
176 Error::DuplicateEntryKeys => "Duplicate keys in this push",
177 Error::InvalidOwners => "Invalid owners",
178 Error::InvalidSuccessor(_) => "Invalid data successor",
179 Error::InvalidOwnersSuccessor(_) => "Invalid owners successor",
180 Error::OpNotCausallyReady => "Operation's is currently not causally ready",
181 Error::InvalidOperation => "Invalid operation",
182 Error::SigningKeyTypeMismatch => "Key type and signature type mismatch",
183 Error::InvalidSignature => "Invalid signature",
184 Error::NetworkOther(ref error) => error,
185 Error::LossOfPrecision => "Lost precision on the amount of money during parsing",
186 Error::ExcessiveValue => {
187 "Overflow on amount of money (check the MAX_MONEY_VALUE const)"
188 }
189 Error::FailedToParse(_) => "Failed to parse entity",
190 Error::TransferIdExists => "Transfer with a given ID already exists",
191 Error::InsufficientBalance => "Not enough money to complete this operation",
192 Error::NoSuchBalance => "Balance does not exist",
193 Error::NoSuchSender => "Sender does not exist",
194 Error::NoSuchRecipient => "Recipient does not exist",
195 Error::BalanceExists => "Balance already exists",
196 Error::DuplicateMessageId => "MessageId already exists",
197 Error::ExceededSize => "Exceeded the size limit",
198 Error::Unexpected(_) => "Unexpected error",
199 }
200 }
201}
202
203#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
205pub enum EntryError {
206 NoSuchEntry,
208 EntryExists(u8),
210 InvalidSuccessor(u8),
212}