Skip to main content

ndt7_client/
error.rs

1//! Error types for the ndt7 client.
2
3use crate::client::AddressFamily;
4use thiserror::Error;
5
6/// Errors that can occur during ndt7 operations.
7#[derive(Debug, Error)]
8pub enum Ndt7Error {
9    /// The Locate API HTTP request failed.
10    #[error("locate failed: {0}")]
11    LocateFailed(#[from] reqwest::Error),
12    /// The Locate API returned no test targets.
13    #[error("no targets available")]
14    NoTargets,
15    /// The Locate API returned 204: M-Lab is out of capacity.
16    #[error("server at capacity; try again later")]
17    NoCapacity,
18    /// JSON serialization or deserialization failed.
19    #[error("serialize/deserialize error: {0}")]
20    JsonError(#[from] serde_json::Error),
21    /// A test exceeded its time limit.
22    #[error("timeout occured")]
23    Timeout(#[from] tokio::time::error::Elapsed),
24    /// A WebSocket-level error occurred.
25    #[error("websocket error: {0}")]
26    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
27    /// The provided service URL path is not a recognized ndt7 endpoint.
28    #[error("bad service URL: {0}")]
29    ServiceUnsupported(String),
30    /// The URL could not be parsed.
31    #[error("URL parse error: {0}")]
32    UrlParse(#[from] url::ParseError),
33    /// An I/O error occurred.
34    #[error("I/O error: {0}")]
35    IoError(#[from] std::io::Error),
36    /// Protocol violation
37    #[error("protocol violation: {0}")]
38    ProtocolViolation(String),
39    /// No addresses of the requested IP family were found for the host.
40    #[error("no {0} address found")]
41    NoAddressFound(AddressFamily),
42}
43
44// Reducing size of Ndt7Error by boxing the large tungstenite::Error variant.
45impl From<tokio_tungstenite::tungstenite::Error> for Ndt7Error {
46    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
47        Ndt7Error::WebSocket(Box::new(e))
48    }
49}
50
51/// A `Result` type alias using [`Ndt7Error`].
52pub type Result<T> = std::result::Result<T, Ndt7Error>;