vector/common/http/
error.rs1use std::{error::Error, fmt};
2
3use serde::Serialize;
4
5#[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 #[allow(unused)] pub fn new(code: http::StatusCode, message: String) -> Self {
22 ErrorMessage {
23 code: code.as_u16(),
24 message,
25 }
26 }
27
28 #[allow(unused)] 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 pub const fn code(&self) -> u16 {
39 self.code
40 }
41
42 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 {}