Skip to content

Commit 71a54d8

Browse files
authored
protocols/kad: Implement Error and Clone for error types (#2414)
1 parent a29c35f commit 71a54d8

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

protocols/kad/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }
2929
void = "1.0"
3030
futures-timer = "3.0.2"
3131
instant = "0.1.11"
32+
thiserror = "1"
3233

3334
[dev-dependencies]
3435
env_logger = "0.9.0"

protocols/kad/src/behaviour.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use std::num::NonZeroUsize;
5555
use std::task::{Context, Poll};
5656
use std::vec;
5757
use std::{borrow::Cow, time::Duration};
58+
use thiserror::Error;
5859

5960
pub use crate::query::QueryStats;
6061

@@ -2583,14 +2584,16 @@ pub struct PutRecordOk {
25832584
}
25842585

25852586
/// The error result of [`Kademlia::put_record`].
2586-
#[derive(Debug, Clone)]
2587+
#[derive(Debug, Clone, Error)]
25872588
pub enum PutRecordError {
2589+
#[error("the quorum failed; needed {quorum} peers")]
25882590
QuorumFailed {
25892591
key: record::Key,
25902592
/// [`PeerId`]s of the peers the record was successfully stored on.
25912593
success: Vec<PeerId>,
25922594
quorum: NonZeroUsize,
25932595
},
2596+
#[error("the request timed out")]
25942597
Timeout {
25952598
key: record::Key,
25962599
/// [`PeerId`]s of the peers the record was successfully stored on.
@@ -2629,8 +2632,9 @@ pub struct BootstrapOk {
26292632
}
26302633

26312634
/// The error result of [`Kademlia::bootstrap`].
2632-
#[derive(Debug, Clone)]
2635+
#[derive(Debug, Clone, Error)]
26332636
pub enum BootstrapError {
2637+
#[error("the request timed out")]
26342638
Timeout {
26352639
peer: PeerId,
26362640
num_remaining: Option<u32>,
@@ -2648,8 +2652,9 @@ pub struct GetClosestPeersOk {
26482652
}
26492653

26502654
/// The error result of [`Kademlia::get_closest_peers`].
2651-
#[derive(Debug, Clone)]
2655+
#[derive(Debug, Clone, Error)]
26522656
pub enum GetClosestPeersError {
2657+
#[error("the request timed out")]
26532658
Timeout { key: Vec<u8>, peers: Vec<PeerId> },
26542659
}
26552660

@@ -2682,8 +2687,9 @@ pub struct GetProvidersOk {
26822687
}
26832688

26842689
/// The error result of [`Kademlia::get_providers`].
2685-
#[derive(Debug, Clone)]
2690+
#[derive(Debug, Clone, Error)]
26862691
pub enum GetProvidersError {
2692+
#[error("the request timed out")]
26872693
Timeout {
26882694
key: record::Key,
26892695
providers: HashSet<PeerId>,
@@ -2718,9 +2724,9 @@ pub struct AddProviderOk {
27182724
}
27192725

27202726
/// The possible errors when publishing a provider record.
2721-
#[derive(Debug, Clone)]
2727+
#[derive(Debug, Clone, Error)]
27222728
pub enum AddProviderError {
2723-
/// The query timed out.
2729+
#[error("the request timed out")]
27242730
Timeout { key: record::Key },
27252731
}
27262732

protocols/kad/src/record/store.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
mod memory;
2222

2323
pub use memory::{MemoryStore, MemoryStoreConfig};
24+
use thiserror::Error;
2425

2526
use super::*;
2627
use crate::K_VALUE;
@@ -30,14 +31,18 @@ use std::borrow::Cow;
3031
pub type Result<T> = std::result::Result<T, Error>;
3132

3233
/// The possible errors of a `RecordStore` operation.
33-
#[derive(Debug, Clone)]
34+
#[derive(Error, Debug, Clone)]
3435
pub enum Error {
3536
/// The store is at capacity w.r.t. the total number of stored records.
37+
#[error("the store cannot contain any more records")]
3638
MaxRecords,
37-
/// The store is at capacity w.r.t. the total number of stored keys for
38-
/// provider records.
39+
40+
/// The store is at capacity w.r.t. the total number of stored provider records.
41+
#[error("the store cannot contain any more provider records")]
3942
MaxProvidedKeys,
40-
/// The value of a record to be stored is too large.
43+
44+
/// The store cannot store this value because it is too large.
45+
#[error("the value is too large to be stored")]
4146
ValueTooLarge,
4247
}
4348

0 commit comments

Comments
 (0)