vector/common/http/
error.rs

1use std::{error::Error, fmt};
2
3use serde::Serialize;
4
5/// HTTP error, containing HTTP status code and a message
6#[derive(Serialize, Debug)]
7pub struct ErrorMessage {
8    code: u16,
9    message: String,
10}
11
12#[cfg(any(
13    feature = "sources-utils-http-prelude",
14    feature = "sources-utils-http-auth",
15    feature = "sources-utils-http-encoding",
16    feature = "sources-datadog_agent"
17))]
18impl ErrorMessage {
19    /// Create a new `ErrorMessage` from HTTP status code and a message
20    #[allow(unused)] // triggered by check-component-features
21    pub fn new(code: http::StatusCode, message: String) -> Self {
22        ErrorMessage {
23            code: code.as_u16(),
24            message,
25        }
26    }
27
28    /// Returns the HTTP status code
29    #[allow(unused)] // triggered by check-component-features
30    pub fn status_code(&self) -> http::StatusCode {
31        http::StatusCode::from_u16(self.code).unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR)
32    }
33}
34
35#[cfg(feature = "sources-utils-http-prelude")]
36impl ErrorMessage {
37    /// Returns the raw HTTP status code
38    pub const fn code(&self) -> u16 {
39        self.code
40    }
41
42    /// Returns the error message
43    pub fn message(&self) -> &str {
44        self.message.as_str()
45    }
46}
47
48impl Error for ErrorMessage {}
49
50impl fmt::Display for ErrorMessage {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        write!(f, "{}: {}", self.code, self.message)
53    }
54}
55
56impl warp::reject::Reject for ErrorMessage {}