Skip to main content

better_fetch/
miette_diagnostic.rs

1//! Rich diagnostics for [`Error`](crate::Error) when the `miette` feature is enabled.
2
3use http::Method;
4use miette::Diagnostic;
5use thiserror::Error;
6use url::Url;
7
8use crate::error::Error;
9
10/// Wraps a fetch [`Error`] with request context for [`miette`](https://docs.rs/miette) reporting.
11#[derive(Debug, Error, Diagnostic)]
12#[error("{inner}")]
13pub struct DiagnosticError {
14    #[source]
15    inner: Error,
16    #[help]
17    method: Option<String>,
18    #[help]
19    url: Option<String>,
20}
21
22impl DiagnosticError {
23    /// Attaches optional HTTP method and URL to `error` for prettier reports.
24    pub fn new(error: Error, method: Option<&Method>, url: Option<&Url>) -> Self {
25        Self {
26            inner: error,
27            method: method.map(|m| m.to_string()),
28            url: url.map(|u| u.to_string()),
29        }
30    }
31}
32
33impl From<Error> for DiagnosticError {
34    fn from(inner: Error) -> Self {
35        Self {
36            inner,
37            method: None,
38            url: None,
39        }
40    }
41}