use super::Prefix;
use crate::messaging::data::Error as ErrorMessage;
use crate::types::{convert_dt_error_to_error_message, DataAddress, PublicKey};
use secured_linked_list::error::Error as SecuredLinkedListError;
use std::io;
use std::net::SocketAddr;
use thiserror::Error;
use xor_name::XorName;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Only messages requiring auth accumultion should be sent via \"send_messages_to_all_nodes_or_directly_handle_for_accumulation\"")]
SendOrHandlingNormalMsg,
#[error("There was a problem during acquisition of a tokio::sync::semaphore permit.")]
PermitAcquisitionFailed,
#[error("Section authority provider cannot be trusted: {0}")]
UntrustedSectionAuthProvider(String),
#[error("Proof chain cannot be trusted: {0}")]
UntrustedProofChain(String),
#[error("Invalid genesis key of provided prefix map: {}", hex::encode(_0.to_bytes()))]
InvalidGenesisKey(bls::PublicKey),
#[error("Cannot route. Delivery group size: {}, candidates: {}.", _0, _1)]
CannotRoute(usize, usize),
#[error("Empty recipient list")]
EmptyRecipientList,
#[error("Could not connect to any bootstrap contact")]
BootstrapFailed,
#[error("Cannot connect to the endpoint: {err}")]
CannotConnectEndpoint {
#[from]
err: qp2p::EndpointError,
},
#[error("Address not reachable: {err}")]
AddressNotReachable {
#[from]
err: qp2p::RpcError,
},
#[error("The node is not in a state to handle the action.")]
InvalidState,
#[error("Invalid source location")]
InvalidSrcLocation,
#[error("Invalid destination location: {0}")]
InvalidDstLocation(String),
#[error("Content of a received message is inconsistent.")]
InvalidMessage,
#[error("A signature share is invalid.")]
InvalidSignatureShare,
#[error("The secret key share is missing for public key {0:?}")]
MissingSecretKeyShare(bls::PublicKey),
#[error("Failed to send a message to {0}, {1}")]
FailedSend(SocketAddr, XorName),
#[error("Connection closed locally")]
ConnectionClosed,
#[error("Invalid section chain: {0}")]
InvalidSectionChain(#[from] SecuredLinkedListError),
#[error("Messaging protocol error: {0}")]
Messaging(#[from] crate::messaging::Error),
#[error("invalid payload")]
InvalidPayload,
#[error("Routing is set to not allow taking any new node")]
TryJoinLater,
#[error("No matching Section")]
NoMatchingSection,
#[error("No matching Elder")]
NoMatchingElder,
#[error("Node cannot join the network since it is not externally reachable: {0}")]
NodeNotReachable(SocketAddr),
#[error("Database error:: {0}")]
Database(#[from] crate::dbs::Error),
#[error("Not enough Adults available in Section({0:?}) to perform operation")]
NoAdults(Prefix),
#[error("Not section public key returned from routing")]
NoSectionPublicKey,
#[error("Not section public key set returned from routing")]
NoSectionPublicKeySet,
#[error("Not section public key returned from routing for xorname {0}")]
NoSectionPublicKeyKnown(XorName),
#[error("No such data: {0:?}")]
NoSuchData(DataAddress),
#[error("Could not create temp store: {0}")]
TempDirCreationFailed(String),
#[error("Data already exists at this node")]
DataExists,
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("JSON serialisation error:: {0}")]
JsonSerialisation(#[from] serde_json::Error),
#[error("Bincode error:: {0}")]
Bincode(#[from] bincode::Error),
#[error("Network service message error:: {0}")]
ServiceMsg(#[from] crate::messaging::data::Error),
#[error("Network data error:: {0}")]
NetworkData(#[from] crate::types::Error),
#[error("Provided PublicKey is not a valid owner. Provided PublicKey: {0}")]
InvalidOwner(PublicKey),
#[error("Invalid operation: {0}")]
InvalidOperation(String),
#[error("No mapping to sn_messages error is set up for this NodeError {0}")]
NoErrorMapping(String),
#[error("Configuration error: {0}")]
Configuration(String),
#[error("Invalid node authority received for a QueryResponse message")]
InvalidQueryResponseAuthority,
}
impl From<qp2p::ClientEndpointError> for Error {
fn from(error: qp2p::ClientEndpointError) -> Self {
Self::CannotConnectEndpoint {
err: match error {
qp2p::ClientEndpointError::Config(error) => qp2p::EndpointError::Config(error),
qp2p::ClientEndpointError::Socket(error) => qp2p::EndpointError::Socket(error),
},
}
}
}
impl From<qp2p::SendError> for Error {
fn from(error: qp2p::SendError) -> Self {
Self::AddressNotReachable {
err: qp2p::RpcError::Send(error),
}
}
}
pub(crate) fn convert_to_error_message(error: Error) -> ErrorMessage {
match error {
Error::InvalidOperation(msg) => ErrorMessage::InvalidOperation(msg),
Error::InvalidOwner(key) => ErrorMessage::InvalidOwner(key),
Error::NoSuchData(address) => ErrorMessage::DataNotFound(address),
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))
}
}
}