Skip to main content

http_extract/
error.rs

1//! Crate-wide, value-redacting extraction errors.
2//!
3//! Errors identify the HTTP field when possible while deliberately excluding
4//! field values and parser details. This keeps formatting safe for operational
5//! logs, including when the failed field contains credentials.
6
7use http::HeaderName;
8
9/// An error produced while extracting request metadata.
10///
11/// The public taxonomy is intentionally small. Parser details, header values,
12/// and credentials are absent from every variant, so formatting an error cannot
13/// disclose request secrets.
14#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
15#[non_exhaustive]
16pub enum Error {
17    /// A field is malformed or cannot be decoded; its value is omitted.
18    #[error("invalid header {name}")]
19    InvalidHeader {
20        /// The malformed field name.
21        name: HeaderName,
22    },
23    /// A field defined by an extractor as singular occurred more than once.
24    #[error("header {name} occurs more than once")]
25    DuplicateHeader {
26        /// The duplicated field name.
27        name: HeaderName,
28    },
29    /// A configured Header name is unsupported by the selected operation.
30    #[error("unsupported header {name}")]
31    UnsupportedHeaderName {
32        /// The unsupported field name.
33        name: String,
34    },
35}
36
37impl Error {
38    #[cfg(feature = "client-ip")]
39    pub(crate) fn unsupported_header_name(name: &str) -> Self {
40        Self::UnsupportedHeaderName {
41            name: name.to_string(),
42        }
43    }
44
45    pub(crate) const fn invalid_header(name: HeaderName) -> Self {
46        Self::InvalidHeader { name }
47    }
48
49    pub(crate) const fn duplicate_header(name: HeaderName) -> Self {
50        Self::DuplicateHeader { name }
51    }
52}