use crate::messaging::data::Error as ErrorMessage;
use crate::types::{convert_dt_error_to_error_message, DataAddress, PublicKey};
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("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("Network data error:: {0}")]
NetworkData(#[from] crate::types::Error),
}
pub(crate) fn convert_to_error_message(error: Error) -> ErrorMessage {
match error {
Error::NotEnoughSpace => ErrorMessage::FailedToWriteFile,
Error::DataIdNotFound(address) => ErrorMessage::DataNotFound(address),
Error::NoSuchData(address) => ErrorMessage::DataNotFound(address),
Error::ChunkNotFound(xorname) => ErrorMessage::ChunkNotFound(xorname),
Error::TempDirCreationFailed(_) => ErrorMessage::FailedToWriteFile,
Error::DataExists => ErrorMessage::DataExists,
Error::NetworkData(error) => convert_dt_error_to_error_message(error),
other => {
ErrorMessage::InvalidOperation(format!("Failed to perform operation: {:?}", other))
}
}
}