ant_protocol/
error.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::{NetworkAddress, PrettyPrintRecordKey};
10use libp2p::kad::store;
11use serde::{Deserialize, Serialize};
12use thiserror::Error;
13
14/// A specialised `Result` type for protocol crate.
15pub type Result<T> = std::result::Result<T, Error>;
16
17/// Main error types for the SAFE protocol.
18#[derive(Error, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
19#[non_exhaustive]
20pub enum Error {
21    // ---------- Misc errors
22    #[error("Could not obtain user's data directory")]
23    UserDataDirectoryNotObtainable,
24    #[error("Could not obtain port from MultiAddr")]
25    CouldNotObtainPortFromMultiAddr,
26    #[error("Could not parse RetryStrategy")]
27    ParseRetryStrategyError,
28    #[error("Could not obtain data dir")]
29    CouldNotObtainDataDir,
30
31    // ---------- Chunk Proof errors
32    #[error("Chunk does not exist {0:?}")]
33    ChunkDoesNotExist(NetworkAddress),
34
35    // ---------- Scratchpad errors
36    /// The provided String can't be deserialized as a ScratchpadAddress
37    #[error("Failed to deserialize hex ScratchpadAddress")]
38    ScratchpadHexDeserializeFailed,
39    /// The provided SecretyKey failed to decrypt the data
40    #[error("Failed to derive CipherText from encrypted_data")]
41    ScratchpadCipherTextFailed,
42    /// The provided cypher text is invalid
43    #[error("Provided cypher text is invalid")]
44    ScratchpadCipherTextInvalid,
45
46    // ---------- payment errors
47    #[error("There was an error getting the storecost from kademlia store")]
48    GetStoreQuoteFailed,
49    #[error("There was an error generating the payment quote")]
50    QuoteGenerationFailed,
51
52    // ---------- replication errors
53    /// Replication not found.
54    #[error("Peer {holder:?} cannot find Record {key:?}")]
55    ReplicatedRecordNotFound {
56        /// Holder that being contacted
57        holder: Box<NetworkAddress>,
58        /// Key of the missing record
59        key: Box<NetworkAddress>,
60    },
61
62    // ---------- record errors
63    // Could not Serialize/Deserialize RecordHeader from Record
64    #[error("Could not Serialize/Deserialize RecordHeader to/from Record")]
65    RecordHeaderParsingFailed,
66    // Could not Serialize/Deserialize Record
67    #[error("Could not Serialize/Deserialize Record")]
68    RecordParsingFailed,
69    // The record already exists at this node
70    #[error("The record already exists, so do not charge for it: {0:?}")]
71    RecordExists(PrettyPrintRecordKey<'static>),
72}
73
74impl From<Error> for store::Error {
75    fn from(_err: Error) -> Self {
76        store::Error::ValueTooLarge
77    }
78}
79
80impl From<store::Error> for Error {
81    fn from(_err: store::Error) -> Self {
82        Error::RecordParsingFailed
83    }
84}