From c427af818696895274b9167ef19d951924b5848b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 13 Nov 2024 07:28:11 +0100 Subject: [PATCH] `hostmeta`: Use `thiserror` --- acrate-hostmeta/Cargo.toml | 1 + acrate-hostmeta/src/lib.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/acrate-hostmeta/Cargo.toml b/acrate-hostmeta/Cargo.toml index bda02e5..14f9f1d 100644 --- a/acrate-hostmeta/Cargo.toml +++ b/acrate-hostmeta/Cargo.toml @@ -9,6 +9,7 @@ quick-xml = { version = "0.37.0", features = ["overlapped-lists", "serialize"] } 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-hostmeta/src/lib.rs b/acrate-hostmeta/src/lib.rs index 57ecd2b..6c61044 100644 --- a/acrate-hostmeta/src/lib.rs +++ b/acrate-hostmeta/src/lib.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; use serde::Deserialize; +use thiserror::Error; /// A resource descriptor object. /// @@ -477,19 +478,22 @@ impl ResourceDescriptor { } /// Error occurred during [`ResourceDescriptor::discover`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ResourceDescriptorDiscoveryError { /// Manipulation of the provided base [`reqwest::Url`] failed. /// /// See [reqwest::Url::set_scheme] for possible causes. + #[error("manipulation of the provided URL failed")] UrlManipulation(()), /// All attempts of fetching a resource descriptor document failed. + #[error("fetchign the resource descriptor document failed")] Fetch(ResourceDescriptorDiscoveryFailures), } /// Request errors occurred during [`ResourceDescriptor::discover`]. -#[derive(Debug)] +#[derive(Debug, Error)] +#[error("all attempts of fetching the resource descriptor document failed")] pub struct ResourceDescriptorDiscoveryFailures { /// HTTPS XRD retrieval. pub https_xrd: GetXRDError, @@ -511,30 +515,46 @@ pub struct ResourceDescriptorDiscoveryFailures { } /// Error occurred during [`ResourceDescriptor::get_xrd`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum GetXRDError { /// 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 read as text. + + /// The document failed to be decoded as text. + #[error("the document failed to be decoded as text")] Decode(reqwest::Error), + /// The document failed to be parsed as XML by [`quick_xml`]. + #[error("the document failed to be parsed as XML")] Parse(quick_xml::DeError), } /// Error occurred during [`ResourceDescriptor::get_jrd`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum GetJRDError { /// 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), }