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
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use std::collections::BTreeMap;
use std::error::Error;
use std::fmt::{self, Display, Formatter};

/// Errors in operations involving Core and Vaults
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum ClientError {
    /// Access is denied for a given requester
    AccessDenied,
    /// SAFE Account does not exist for client
    NoSuchAccount,
    /// Attempt to take an account network name that already exists
    AccountExists,
    /// Requested data not found
    NoSuchData,
    /// Attempt to create a mutable data when data with such a name already exists
    DataExists,
    /// Attempt to create/post a data exceeds size limit
    DataTooLarge,
    /// Requested entry not found
    NoSuchEntry,
    /// Exceeded a limit on a number of entries
    TooManyEntries,
    /// Some entry actions are not valid.
    InvalidEntryActions(BTreeMap<Vec<u8>, EntryError>),
    /// Key does not exist
    NoSuchKey,
    /// The list of owner keys is invalid
    InvalidOwners,
    /// Invalid version for performing a given mutating operation. Contains the
    /// current data version.
    InvalidSuccessor(u64),
    /// Invalid Operation such as a POST on ImmutableData
    InvalidOperation,
    /// Wrong invitation token specified by the client
    InvalidInvitation,
    /// Invitation token already used
    InvitationAlreadyClaimed,
    /// Insufficient balance for performing a given mutating operation
    LowBalance,
    /// The loss of sacrificial copies indicates the network as a whole is no longer having
    /// enough space to accept further put request so have to wait for more nodes to join
    NetworkFull,
    /// Network error occurring at Vault level which has no bearing on clients, e.g. serialisation
    /// failure or database failure
    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,
        }
    }
}

/// Entry error for `ClientError::InvalidEntryActions`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum EntryError {
    /// Entry does not exists.
    NoSuchEntry,
    /// Entry already exists. Contains the current entry version.
    EntryExists(u64),
    /// Invalid version when updating an entry. Contains the current entry version.
    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 = unwrap_err!(mutate());
        match (err_get, err_mutation) {
            (ClientError::NetworkOther(val0), ClientError::NetworkOther(val1)) => {
                assert_eq!(&val0, "Get");
                assert_eq!(&val1, "Mutation");
            }
            err => panic!("Unexpected conversion: {:?}", err),
        }
    }
}