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
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.1.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// 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::error::Error;
use std::fmt::{self, Display, Formatter};

/// Errors in Get (non-mutating) operations involving Core and Vaults
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum GetError {
    /// SAFE Account does not exist for client
    NoSuchAccount,
    /// Requested data not found
    NoSuchData,
    /// 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 GetError {
    fn from(err: T) -> Self {
        GetError::NetworkOther(err.into())
    }
}

impl Display for GetError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match *self {
            GetError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
            GetError::NoSuchData => write!(formatter, "Requested data not found"),
            GetError::NetworkOther(ref error) => {
                write!(formatter, "Error on Vault network: {}", error)
            }
        }
    }
}

impl Error for GetError {
    fn description(&self) -> &str {
        match *self {
            GetError::NoSuchAccount => "No such account",
            GetError::NoSuchData => "No such data",
            GetError::NetworkOther(ref error) => error,
        }
    }
}



/// Errors in Put/Post/Delete (mutating) operations involving Core and Vaults
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub enum MutationError {
    /// 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,
    /// Insufficient balance for performing a given mutating operation
    LowBalance,
    /// Invalid successor for performing a given mutating operation, e.g. signature mismatch or
    /// invalid data versioning
    InvalidSuccessor,
    /// Invalid Operation such as a POST on ImmutableData
    InvalidOperation,
    /// 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 MutationError {
    fn from(err: T) -> Self {
        MutationError::NetworkOther(err.into())
    }
}

impl Display for MutationError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match *self {
            MutationError::NoSuchAccount => write!(formatter, "Account does not exist for client"),
            MutationError::AccountExists => write!(formatter, "Account already exists for client"),
            MutationError::NoSuchData => write!(formatter, "Requested data not found"),
            MutationError::DataExists => write!(formatter, "Data given already exists"),
            MutationError::DataTooLarge => write!(formatter, "Data given is too large"),
            MutationError::LowBalance => {
                write!(formatter, "Insufficient account balance for this operation")
            }
            MutationError::InvalidSuccessor => {
                write!(formatter,
                       "Data given is not a valid successor of stored data")
            }
            MutationError::InvalidOperation => {
                write!(formatter, "Requested operation is not allowed")
            }
            MutationError::NetworkFull => {
                write!(formatter, "Network cannot store any further data")
            }
            MutationError::NetworkOther(ref error) => {
                write!(formatter, "Error on Vault network: {}", error)
            }
        }
    }
}

impl Error for MutationError {
    fn description(&self) -> &str {
        match *self {
            MutationError::NoSuchAccount => "No such account",
            MutationError::AccountExists => "Account exists",
            MutationError::NoSuchData => "No such data",
            MutationError::DataExists => "Data exists",
            MutationError::DataTooLarge => "Data is too large",
            MutationError::LowBalance => "Low account balance",
            MutationError::InvalidSuccessor => "Invalid data successor",
            MutationError::InvalidOperation => "Invalid operation",
            MutationError::NetworkFull => "Network full",
            MutationError::NetworkOther(ref error) => error,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn conversion_from_str_literal() {
        fn mutate() -> Result<(), MutationError> {
            Err("Mutation")?
        }

        let err_get = GetError::from("Get");
        let err_mutation = mutate().unwrap_err();
        match (err_get, err_mutation) {
            (GetError::NetworkOther(val0), MutationError::NetworkOther(val1)) => {
                assert_eq!(&val0, "Get");
                assert_eq!(&val1, "Mutation");
            }
            err => panic!("Unexpected conversion: {:?}", err),
        }
    }
}