use sn_interface::messaging::data::Error as ErrorMsg;
use sn_interface::types::{
convert_dt_error_to_error_msg, DataAddress, PublicKey, ReplicatedDataAddress,
};
use std::io;
use thiserror::Error;
use xor_name::XorName;
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
#[allow(clippy::large_enum_variant)]
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("Could not convert the Db key")]
CouldNotConvertDbKey,
#[error("Could not decode the Db key: {0:?}")]
CouldNotDecodeDbKey(String),
#[error("Not enough space")]
NotEnoughSpace,
#[error("Key not found: {0:?}")]
KeyNotFound(String),
#[error("No value found for key: {0:?}")]
NoSuchValue(String),
#[error("Data id not found: {0:?}")]
DataIdNotFound(DataAddress),
#[error("Cannot delete public data {0:?}")]
CannotDeletePublicData(DataAddress),
#[error("No such data: {0:?}")]
NoSuchData(DataAddress),
#[error("No such data for replication: {0:?}")]
NoSuchDataForReplication(ReplicatedDataAddress),
#[error("Chunk not found: {0:?}")]
ChunkNotFound(XorName),
#[error("Data already exists at this node")]
DataExists,
#[error("Provided PublicKey is not a valid owner. Provided PublicKey: {0}")]
InvalidOwner(PublicKey),
#[error("A KV store was loaded, but found to be invalid")]
InvalidStore,
#[error("Provided PublicKey could not validate signature {0:?}")]
InvalidSignature(PublicKey),
#[error("Serialization error: {0}")]
Serialize(String),
#[error("Deserialization error: {0}")]
Deserialize(String),
#[error("Could not create temp store: {0}")]
TempDirCreationFailed(String),
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Bincode error:: {0}")]
Bincode(#[from] bincode::Error),
#[error("Could not parse key:: {0:?}")]
CouldNotParseDbKey(Vec<u8>),
#[error("Operation Id could not be generated")]
NoOperationId,
#[error("Sled error:: {0}")]
Sled(#[from] sled::Error),
#[error("Errors found when batching for Sled")]
SledBatching,
#[error("Invalid chunk filename")]
InvalidFilename,
#[error("Network data error:: {0}")]
NetworkData(#[from] sn_interface::types::Error),
#[error("Path contains no file name")]
NoFilename,
}
pub(crate) fn convert_to_error_msg(error: Error) -> ErrorMsg {
match error {
Error::NotEnoughSpace => ErrorMsg::FailedToWriteFile,
Error::DataIdNotFound(address) => ErrorMsg::DataNotFound(address),
Error::NoSuchData(address) => ErrorMsg::DataNotFound(address),
Error::ChunkNotFound(xorname) => ErrorMsg::ChunkNotFound(xorname),
Error::TempDirCreationFailed(_) => ErrorMsg::FailedToWriteFile,
Error::DataExists => ErrorMsg::DataExists,
Error::NetworkData(error) => convert_dt_error_to_error_msg(error),
other => ErrorMsg::InvalidOperation(format!("Failed to perform operation: {:?}", other)),
}
}