use derec_proto::SenderKind;
use crate::derec_message::DeRecMessageBuilderError;
use crate::primitives::{
discovery::DiscoveryError, pairing::PairingError, recovery::RecoveryError,
sharing::SharingError, unpairing::UnpairingError, verification::VerificationError,
};
use crate::protocol::{
ChannelStoreError, RestoreError, SecretStoreError, ShareStoreError, StateStoreError,
};
use crate::transport::TransportValidationError;
use crate::types::ChannelId;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
Pairing(#[from] PairingError),
#[error(transparent)]
Recovery(#[from] RecoveryError),
#[error(transparent)]
Discovery(#[from] DiscoveryError),
#[error(transparent)]
Sharing(#[from] SharingError),
#[error(transparent)]
Verification(#[from] VerificationError),
#[error(transparent)]
Unpairing(#[from] UnpairingError),
#[error(transparent)]
Restore(#[from] RestoreError),
#[error(transparent)]
DeRecMessage(#[from] DeRecMessageBuilderError),
#[error(transparent)]
SecretStore(#[from] SecretStoreError),
#[error(transparent)]
ChannelStore(#[from] ChannelStoreError),
#[error(transparent)]
ShareStore(#[from] ShareStoreError),
#[error(transparent)]
StateStore(#[from] StateStoreError),
#[error(transparent)]
Transport(#[from] TransportValidationError),
#[error("invalid input: {0}")]
InvalidInput(&'static str),
#[error("protobuf decode error")]
ProtobufDecode(#[source] prost::DecodeError),
#[error("protobuf encode error")]
ProtobufEncode(#[source] prost::EncodeError),
#[error("internal invariant violated: {0}")]
Invariant(&'static str),
#[error("role mismatch on channel {channel_id:?}: expected {expected:?}, got {actual:?}")]
RoleMismatch {
channel_id: ChannelId,
expected: SenderKind,
actual: SenderKind,
},
#[error("channel {channel_id:?} is already paired")]
ChannelAlreadyPaired { channel_id: ChannelId },
#[error(
"replica id not configured: build the protocol with .with_replica_id(..) to enable replica flows"
)]
ReplicaIdNotConfigured,
#[error("replica id {replica_id} is already in use by another member of the group")]
ReplicaIdConflict { replica_id: u64 },
#[error("fingerprint does not match channel {channel_id:?}: possible man-in-the-middle")]
FingerprintMismatch { channel_id: ChannelId },
#[error("transport send failed: the message reached none of the peer's endpoints")]
TransportSendFailed,
#[error(
"peer advertised no usable transport endpoint; all {offered} offer(s) \
were refused by transport policy"
)]
NoUsableEndpoint {
offered: usize,
},
}
impl Error {
pub fn channel_id(&self) -> Option<ChannelId> {
match self {
Error::ChannelAlreadyPaired { channel_id }
| Error::FingerprintMismatch { channel_id }
| Error::RoleMismatch { channel_id, .. } => Some(*channel_id),
_ => None,
}
}
pub fn as_non_ok_status(&self) -> Option<(i32, &str)> {
match self {
Error::Pairing(PairingError::NonOkStatus { status, memo }) => Some((*status, memo)),
Error::Sharing(SharingError::NonOkStatus { status, memo }) => Some((*status, memo)),
Error::Verification(VerificationError::NonOkStatus { status, memo }) => {
Some((*status, memo))
}
Error::Discovery(DiscoveryError::NonOkStatus { status, memo }) => Some((*status, memo)),
Error::Recovery(RecoveryError::NonOkStatus { status, memo }) => Some((*status, memo)),
Error::Unpairing(UnpairingError::NonOkStatus { status, memo }) => Some((*status, memo)),
_ => None,
}
}
}