Skip to main content

iroh_netbench/
error.rs

1//! Error types.
2
3use std::time::Duration;
4
5/// Crate result type.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors produced by a benchmark run.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// A control frame exceeded the configured maximum.
12    #[error("control message is too large: {actual} bytes (maximum {maximum})")]
13    ControlMessageTooLarge {
14        /// Encoded message size.
15        actual: usize,
16        /// Configured maximum.
17        maximum: usize,
18    },
19
20    /// The peer selected no mutually supported protocol version.
21    #[error("no mutually supported protocol version")]
22    UnsupportedProtocolVersion,
23
24    /// A requested test exceeds a server limit.
25    #[error("test duration {requested:?} exceeds server limit {maximum:?}")]
26    DurationLimitExceeded {
27        /// Requested duration.
28        requested: Duration,
29        /// Server maximum.
30        maximum: Duration,
31    },
32
33    /// The operation timed out.
34    #[error("operation timed out during {stage}")]
35    Timeout {
36        /// Human-readable stage name.
37        stage: &'static str,
38    },
39
40    /// The caller cancelled the benchmark task.
41    #[error("benchmark was cancelled")]
42    Cancelled,
43
44    /// The peer allows low-bandwidth probes but denies throughput measurements.
45    #[error("peer policy denies throughput measurements")]
46    ThroughputDeniedByPeer,
47
48    /// The peer rejected or failed a protocol operation.
49    #[error("peer error {code}: {message}")]
50    Peer {
51        /// Stable wire error code.
52        code: u16,
53        /// Diagnostic message.
54        message: String,
55    },
56
57    /// A host session or stream operation failed.
58    #[error("network error: {0}")]
59    Network(String),
60
61    /// The peer cleanly ended a flow-local measurement stream at its accounting deadline.
62    #[error("measurement stream was stopped by the peer")]
63    FlowStopped,
64
65    /// A protocol invariant was violated.
66    #[error("protocol error: {0}")]
67    Protocol(String),
68
69    /// Control message serialization failed.
70    #[error("control codec error: {0}")]
71    Codec(#[from] postcard::Error),
72
73    /// I/O failed.
74    #[error("I/O error: {0}")]
75    Io(#[from] std::io::Error),
76}
77
78impl Error {
79    /// Converts an external network error without exposing it in the public error enum.
80    pub(crate) fn network(error: impl std::fmt::Display) -> Self {
81        Self::Network(error.to_string())
82    }
83}