mutant_lib/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use autonomi::client::data_types::scratchpad::ScratchpadError;
4// use std::error::Error as StdError; // Unused
5use thiserror::Error;
6use tokio::task::JoinError;
7
8/// Errors that can occur within the mutant-lib.
9#[derive(Error, Debug)]
10pub enum Error {
11    #[error("Network initialization failed: {0}")]
12    NetworkInitError(String),
13    #[error("Wallet creation failed: {0}")]
14    WalletError(String),
15    #[error("Storage operation failed: {0}")]
16    StorageError(String),
17    #[error("Serialization error: {0}")]
18    SerializationError(String),
19    #[error("Deserialization error: {0}")]
20    DeserializationError(String),
21    #[error("Key not found: {0}")]
22    KeyNotFound(String),
23    #[error("Key already exists: {0}")]
24    KeyAlreadyExists(String),
25    #[error("Data too large: {0}")]
26    DataTooLarge(String),
27    #[error("Scratchpad creation failed: {0}")]
28    CreationFailed(String),
29    #[error("Scratchpad fetch failed: {0}")]
30    FetchFailed(String),
31    #[error("Scratchpad update failed: {0}")]
32    UpdateFailed(String),
33    #[error("Scratchpad remove failed: {0}")]
34    RemoveFailed(String),
35    #[error("Direct storage failed: {0}")]
36    DirectStoreFailed(String),
37    #[error("Direct fetch failed: {0}")]
38    DirectFetchFailed(String),
39    #[error("Chunk storage failed: {0}")]
40    ChunkStoreFailed(String),
41    #[error("Failed to fetch chunk {chunk_index} ({address}) for key '{key}': {source}")]
42    ChunkFetchFailed {
43        key: String,
44        chunk_index: usize,
45        address: autonomi::ScratchpadAddress,
46        source: Box<Error>, // Box the underlying error
47    },
48    #[error("Invalid internal state: {0}")]
49    InternalError(String),
50    #[error("Operation not supported")]
51    OperationNotSupported,
52    #[error("Invalid input: {0}")]
53    InvalidInput(String),
54    #[error("Scratchpad fetch failed: {0}")]
55    ScratchpadFetchFailed(String),
56    #[error("Scratchpad read failed: {0}")]
57    ScratchpadReadFailed(String),
58    #[error("Invalid read range requested for scratchpad: {0}")]
59    InvalidReadRange(String),
60    #[error("Allocation failed: {0}")]
61    AllocationFailed(String),
62    #[error("Deallocation failed: {0}")]
63    DeallocationFailed(String),
64    #[error("Insufficient allocated space: {0}")]
65    InsufficientSpace(String),
66    #[error("Operation cancelled by user or callback")]
67    OperationCancelled,
68    #[error("Failed to upload data")]
69    FailedToUploadData,
70    #[error("Failed to retrieve data")]
71    FailedToRetrieveData,
72    #[error("Failed to acquire lock")]
73    LockError,
74    #[error("Failed to connect to network: {0}")]
75    NetworkConnectionFailed(String),
76    #[error("Failed to create wallet: {0}")]
77    WalletCreationFailed(String),
78    #[error("Failed to derive vault key: {0}")]
79    VaultKeyDerivationFailed(String),
80    #[error("Failed to fetch from vault: {0}")]
81    VaultFetchFailed(String),
82    #[error("Failed to store to vault: {0}")]
83    VaultStoreFailed(String),
84    #[error("Failed to initialize storage")]
85    StorageInitializationFailed,
86    #[error("Pack management error: {0}")]
87    PackManagementError(String),
88    #[error("Internal key not found within data pack")]
89    ItemNotInPack,
90    #[error("Feature not implemented: {0}")]
91    NotImplemented(String),
92    #[error("Invalid data location found in index")]
93    InvalidDataLocation,
94    #[error("CBOR serialization/deserialization error: {0}")]
95    Cbor(#[from] serde_cbor::Error),
96    #[error("I/O error: {0}")]
97    Io(#[from] std::io::Error),
98    #[error("Autonomi scratchpad client error: {0}")]
99    AutonomiClient(#[from] ScratchpadError),
100    #[error("Task join error: {0}")]
101    TaskJoinError(String),
102    #[error("Vault write failed: {0}")]
103    VaultWriteFailed(String),
104    #[error("Scratchpad get failed: {0}")]
105    GetFailed(String),
106    #[error("Scratchpad delete failed: {0}")]
107    DeleteFailed(String),
108    #[error("Allocator error: {0}")]
109    AllocatorError(String),
110    #[error("Network error: {0}")]
111    NetworkError(String),
112    #[error("Invalid argument: {0}")]
113    InvalidArgument(String),
114    #[error(
115        "Reconstructed data for key '{key}' is incomplete: expected {expected} bytes, got {actual}"
116    )]
117    IncompleteData {
118        key: String,
119        expected: u64,
120        actual: u64,
121    },
122    #[error("Dialoguer interaction error: {0}")]
123    DialoguerError(String),
124    #[error("Operation cancelled by user")]
125    UserCancelled,
126    #[error("Autonomi library error: {0}")]
127    AutonomiLibError(String),
128    #[error("Operation timed out: {0}")]
129    Timeout(String),
130    #[error("Tokio task join error: {0}")]
131    JoinError(String),
132}
133
134impl Error {
135    pub fn from_join_error_msg(join_error: &JoinError, context_msg: String) -> Self {
136        let cause = if join_error.is_panic() {
137            "Task panicked".to_string()
138        } else if join_error.is_cancelled() {
139            "Task cancelled".to_string()
140        } else {
141            "Unknown task failure".to_string()
142        };
143        Error::InternalError(format!("{}: {} ({})", context_msg, cause, join_error))
144    }
145}
146
147impl From<JoinError> for Error {
148    fn from(err: JoinError) -> Self {
149        Error::JoinError(err.to_string())
150    }
151}