protocols/kad: Implement Error and Clone for error types (#2414)

This commit is contained in:
Ibiyemi Abiodun 2022-01-06 20:54:01 -05:00 committed by GitHub
parent a29c35f8e7
commit 71a54d8227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View File

@ -29,6 +29,7 @@ unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }
void = "1.0" void = "1.0"
futures-timer = "3.0.2" futures-timer = "3.0.2"
instant = "0.1.11" instant = "0.1.11"
thiserror = "1"
[dev-dependencies] [dev-dependencies]
env_logger = "0.9.0" env_logger = "0.9.0"

View File

@ -55,6 +55,7 @@ use std::num::NonZeroUsize;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use std::vec; use std::vec;
use std::{borrow::Cow, time::Duration}; use std::{borrow::Cow, time::Duration};
use thiserror::Error;
pub use crate::query::QueryStats; pub use crate::query::QueryStats;
@ -2583,14 +2584,16 @@ pub struct PutRecordOk {
} }
/// The error result of [`Kademlia::put_record`]. /// The error result of [`Kademlia::put_record`].
#[derive(Debug, Clone)] #[derive(Debug, Clone, Error)]
pub enum PutRecordError { pub enum PutRecordError {
#[error("the quorum failed; needed {quorum} peers")]
QuorumFailed { QuorumFailed {
key: record::Key, key: record::Key,
/// [`PeerId`]s of the peers the record was successfully stored on. /// [`PeerId`]s of the peers the record was successfully stored on.
success: Vec<PeerId>, success: Vec<PeerId>,
quorum: NonZeroUsize, quorum: NonZeroUsize,
}, },
#[error("the request timed out")]
Timeout { Timeout {
key: record::Key, key: record::Key,
/// [`PeerId`]s of the peers the record was successfully stored on. /// [`PeerId`]s of the peers the record was successfully stored on.
@ -2629,8 +2632,9 @@ pub struct BootstrapOk {
} }
/// The error result of [`Kademlia::bootstrap`]. /// The error result of [`Kademlia::bootstrap`].
#[derive(Debug, Clone)] #[derive(Debug, Clone, Error)]
pub enum BootstrapError { pub enum BootstrapError {
#[error("the request timed out")]
Timeout { Timeout {
peer: PeerId, peer: PeerId,
num_remaining: Option<u32>, num_remaining: Option<u32>,
@ -2648,8 +2652,9 @@ pub struct GetClosestPeersOk {
} }
/// The error result of [`Kademlia::get_closest_peers`]. /// The error result of [`Kademlia::get_closest_peers`].
#[derive(Debug, Clone)] #[derive(Debug, Clone, Error)]
pub enum GetClosestPeersError { pub enum GetClosestPeersError {
#[error("the request timed out")]
Timeout { key: Vec<u8>, peers: Vec<PeerId> }, Timeout { key: Vec<u8>, peers: Vec<PeerId> },
} }
@ -2682,8 +2687,9 @@ pub struct GetProvidersOk {
} }
/// The error result of [`Kademlia::get_providers`]. /// The error result of [`Kademlia::get_providers`].
#[derive(Debug, Clone)] #[derive(Debug, Clone, Error)]
pub enum GetProvidersError { pub enum GetProvidersError {
#[error("the request timed out")]
Timeout { Timeout {
key: record::Key, key: record::Key,
providers: HashSet<PeerId>, providers: HashSet<PeerId>,
@ -2718,9 +2724,9 @@ pub struct AddProviderOk {
} }
/// The possible errors when publishing a provider record. /// The possible errors when publishing a provider record.
#[derive(Debug, Clone)] #[derive(Debug, Clone, Error)]
pub enum AddProviderError { pub enum AddProviderError {
/// The query timed out. #[error("the request timed out")]
Timeout { key: record::Key }, Timeout { key: record::Key },
} }

View File

@ -21,6 +21,7 @@
mod memory; mod memory;
pub use memory::{MemoryStore, MemoryStoreConfig}; pub use memory::{MemoryStore, MemoryStoreConfig};
use thiserror::Error;
use super::*; use super::*;
use crate::K_VALUE; use crate::K_VALUE;
@ -30,14 +31,18 @@ use std::borrow::Cow;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// The possible errors of a `RecordStore` operation. /// The possible errors of a `RecordStore` operation.
#[derive(Debug, Clone)] #[derive(Error, Debug, Clone)]
pub enum Error { pub enum Error {
/// The store is at capacity w.r.t. the total number of stored records. /// The store is at capacity w.r.t. the total number of stored records.
#[error("the store cannot contain any more records")]
MaxRecords, MaxRecords,
/// The store is at capacity w.r.t. the total number of stored keys for
/// provider records. /// The store is at capacity w.r.t. the total number of stored provider records.
#[error("the store cannot contain any more provider records")]
MaxProvidedKeys, MaxProvidedKeys,
/// The value of a record to be stored is too large.
/// The store cannot store this value because it is too large.
#[error("the value is too large to be stored")]
ValueTooLarge, ValueTooLarge,
} }