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    // ---------- Chunk errors
36    #[error("Chunk is too large: {0} bytes, when max size is {1} bytes")]
37    OversizedChunk(usize, usize),
38
39    // ---------- Scratchpad errors
40    /// The provided String can't be deserialized as a ScratchpadAddress
41    #[error("Failed to deserialize hex ScratchpadAddress")]
42    ScratchpadHexDeserializeFailed,
43    /// The provided SecretyKey failed to decrypt the data
44    #[error("Failed to derive CipherText from encrypted_data")]
45    ScratchpadCipherTextFailed,
46    /// The provided cypher text is invalid
47    #[error("Provided cypher text is invalid")]
48    ScratchpadCipherTextInvalid,
49
50    // ---------- payment errors
51    #[error("There was an error getting the storecost from kademlia store")]
52    GetStoreQuoteFailed,
53    #[error("There was an error generating the payment quote")]
54    QuoteGenerationFailed,
55
56    // ---------- replication errors
57    /// Replication not found.
58    #[error("Peer {holder:?} cannot find Record {key:?}")]
59    ReplicatedRecordNotFound {
60        /// Holder that being contacted
61        holder: Box<NetworkAddress>,
62        /// Key of the missing record
63        key: Box<NetworkAddress>,
64    },
65
66    // ---------- record errors
67    // Could not Serialize/Deserialize RecordHeader from Record
68    #[error("Could not Serialize/Deserialize RecordHeader to/from Record")]
69    RecordHeaderParsingFailed,
70    // Could not Serialize/Deserialize Record
71    #[error("Could not Serialize/Deserialize Record")]
72    RecordParsingFailed,
73    // The record already exists at this node
74    #[error("The record already exists, so do not charge for it: {0:?}")]
75    RecordExists(PrettyPrintRecordKey<'static>),
76}
77
78impl From<Error> for store::Error {
79    fn from(_err: Error) -> Self {
80        store::Error::ValueTooLarge
81    }
82}
83
84impl From<store::Error> for Error {
85    fn from(_err: store::Error) -> Self {
86        Error::RecordParsingFailed
87    }
88}