native_svc/
error.rs

1//! Error types for the Hyper HTTP connection implementation.
2//!
3//! Defines a unified `HyperError` enum wrapping IO, HTTP, and `hyper` library errors,
4//! as well as connector-specific conditions like missing requests/responses and unsupported methods.
5
6use embedded_svc::io::{Error as SvcError, ErrorKind as SvcErrorKind};
7use hyper::http;
8use std::io;
9use hyper::header::{InvalidHeaderName, InvalidHeaderValue};
10use thiserror::Error;
11
12/// A comprehensive error type for the Hyper-based HTTP client.
13///
14/// Wraps errors from various layers:
15///
16/// - `io::Error` for general I/O failures.
17/// - `http::Error` for request building mistakes.
18/// - `hyper::Error` for runtime or protocol-level failures.
19/// - `hyper_util::client::legacy::Error` for client connector issues.
20///
21/// Also includes variants for unsupported HTTP methods, missing requests or responses,
22/// and invalid header names or values.
23#[derive(Error, Debug)]
24pub enum HyperError {
25    /// Underlying I/O error.
26    #[error("io error: {0:?}")]
27    Io(#[from] io::Error),
28
29    /// Error constructing or parsing an HTTP message.
30    #[error("http error: {0:?}")]
31    Http(#[from] http::Error),
32
33    /// Error returned by the Hyper library during request/response processing.
34    #[error("hyper error: {0:?}")]
35    Hyper(#[from] hyper::Error),
36
37    /// Error originating from the Hyper legacy client connector.
38    #[error("client error: {0:?}")]
39    Client(#[from] hyper_util::client::legacy::Error),
40
41    /// Failed to initialize the Tokio runtime.
42    #[error("tokio runtime initialization error: {0:?}")]
43    RuntimeCreation(io::Error),
44
45    /// The connector did not support an HTTP method.
46    #[error("unsupported http method: {0}")]
47    UnsupportedMethod(String),
48
49    /// No HTTP response has been received when expected.
50    #[error("no response initialized")]
51    NoResponse,
52
53    /// No HTTP request has been initiated when expected.
54    #[error("no request initialized")]
55    NoRequest,
56
57    /// A header name provided was invalid, according to HTTP specifications.
58    #[error("invalid header name: {0:?}")]
59    InvalidHeaderName(#[from] InvalidHeaderName),
60
61    /// A header value provided was invalid according to HTTP value rules.
62    #[error("invalid header value: {0:?}")]
63    InvalidHeaderValue(#[from] InvalidHeaderValue),
64}
65
66impl SvcError for HyperError {
67    /// Maps all `HyperError` variants to `ErrorKind::Other` for embedded-svc.
68    fn kind(&self) -> SvcErrorKind {
69        SvcErrorKind::Other
70    }
71}