From 30610df0f518623d7cae78bc0fe14b85c63ddc5c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 13 Nov 2024 07:31:39 +0100 Subject: [PATCH] `nodeinfo`: Use `thiserror` --- acrate-nodeinfo/Cargo.toml | 1 + acrate-nodeinfo/src/lib.rs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/acrate-nodeinfo/Cargo.toml b/acrate-nodeinfo/Cargo.toml index d8ca425..05fa2a3 100644 --- a/acrate-nodeinfo/Cargo.toml +++ b/acrate-nodeinfo/Cargo.toml @@ -9,6 +9,7 @@ log = "0.4.22" reqwest = { version = "0.12.9", features = ["json", "stream"] } serde = { version = "1.0.214", features = ["derive"] } serde_json = "1.0.132" +thiserror = "2.0.3" [dev-dependencies] pretty_env_logger = "0.5.0" diff --git a/acrate-nodeinfo/src/lib.rs b/acrate-nodeinfo/src/lib.rs index 5a11fe2..b04d870 100644 --- a/acrate-nodeinfo/src/lib.rs +++ b/acrate-nodeinfo/src/lib.rs @@ -8,6 +8,7 @@ //! - use serde::Deserialize; +use thiserror::Error; /// A variant of a NodeInfo document. /// @@ -309,11 +310,13 @@ impl NodeInfo { } /// An error occurred during [`NodeInfo::get_latest_wellknown`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum NodeInfoGetWellknownError { /// The discovery of possible locations for NodeInfo documents failed. + #[error("the discovery of possible locations for NodeInfo documents failed")] Discovery(acrate_hostmeta::ResourceDescriptorDiscoveryError), /// No compatible NodeInfo documents were detected at the given URL. + #[error("no compatible NodeInfo documents were detected at the given URL")] Unsupported, } @@ -446,17 +449,26 @@ impl NodeInfo2 { } /// An error encountered during [`NodeInfo1::get`] or [`NodeInfo2::get`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum NodeInfoGetError { /// The HTTP request failed. + #[error("the HTTP request failed")] Request(reqwest::Error), + /// The `Content-Type` header of the response is missing. + #[error("the Content-Type header of the response is missing")] ContentTypeMissing, + /// The `Content-Type` header of the response is invalid. + #[error("the Content-Type header of the response is invalid")] ContentTypeInvalid, + /// The document failed to be parsed as JSON by [`reqwest`]. + #[error("the document failed to be parsed as JSON")] Parse(reqwest::Error), - /// The returned version does not match the version of the created struct. + + /// The returned NodeInfo version would not match the version of the called method. + #[error("the returned NodeInfo version would not match the version of the called method")] Version, }