use {
crate::{
discovery::Error as DiscoveryError,
groups::{Groups, StateMachine},
network::{
self,
link::{Link, LinkError},
},
primitives::EncodeError,
},
core::fmt::Debug,
};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Group has been terminated")]
GroupTerminated,
#[error("Invalid group secret key proof provided")]
InvalidGroupKeyProof,
#[error("Unauthorized")]
Unauthorized,
#[error("Link error: {0}")]
Link(#[from] LinkError),
#[error("An active bond already exists with this peer")]
AlreadyBonded(Box<Link<Groups>>),
#[error("Discovery error: {0}")]
Discovery(DiscoveryError),
}
#[derive(thiserror::Error)]
pub enum CommandError<M: StateMachine> {
#[error("Group is temporarily offline and cannot process commands")]
Offline(Vec<M::Command>),
#[error("No commands provided")]
NoCommands,
#[error("Group is terminated")]
GroupTerminated,
#[error("Command encoding error: {1}")]
Encoding(Vec<M::Command>, EncodeError),
}
impl<M: StateMachine> Debug for CommandError<M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Offline(commands) => f
.debug_tuple("CommandError::Offline")
.field(&format!("{} commands", commands.len()))
.finish(),
Self::NoCommands => f.debug_tuple("CommandError::NoCommands").finish(),
Self::GroupTerminated => {
f.debug_tuple("CommandError::GroupTerminated").finish()
}
Self::Encoding(commands, err) => f
.debug_tuple("CommandError::Encoding")
.field(&format!("{} commands", commands.len()))
.field(err)
.finish(),
}
}
}
#[derive(thiserror::Error)]
pub enum QueryError<M: StateMachine> {
#[error("Group is temporarily offline and cannot process queries")]
Offline(M::Query),
#[error("Group is terminated")]
GroupTerminated,
#[error("Query encoding error: {1}")]
Encoding(M::Query, EncodeError),
}
impl<M: StateMachine> Debug for QueryError<M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Offline(_) => {
f.debug_tuple("QueryError::Offline").finish_non_exhaustive()
}
Self::GroupTerminated => {
f.debug_tuple("QueryError::GroupTerminated").finish()
}
Self::Encoding(_, err) => f
.debug_tuple("QueryError::Encoding")
.field(err)
.finish_non_exhaustive(),
}
}
}
network::make_close_reason!(
pub struct InvalidHandshake, 30_400);
network::make_close_reason!(
pub struct NotAllowed, 30_403);
network::make_close_reason!(
pub struct GroupNotFound, 30_404);
network::make_close_reason!(
pub struct InvalidProof, 30_405);
network::make_close_reason!(
pub struct Timeout, 30_408);
network::make_close_reason!(
pub struct AlreadyBonded, 30_429);