Skip to main content

brooks_lib/dcdn/
error.rs

1// brooks, Copyright 2026, Will Hawkins
2//
3// This file is part of brooks.
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use std::fmt::Display;
19
20use http::StatusCode;
21
22use crate::{dcdn::interpret::Error, logging::LogMsgs};
23
24pub type ProxyValue = (StatusCode, http::Response<Vec<u8>>, LogMsgs);
25pub type ProxyResult = Result<ProxyValue, (Box<ProxyError>, LogMsgs)>;
26
27#[derive(Debug)]
28pub enum ProxyError {
29    SetupError(Box<dyn std::error::Error>),
30    HmdsError(Box<dyn std::error::Error>),
31    ImdiError(ImdInterpretError),
32    NativeError(Box<dyn std::error::Error>),
33}
34
35impl Display for ProxyError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "Error proxying to/from the native request: ")?;
38        match &self {
39            ProxyError::SetupError(error) => write!(f, "{error}"),
40            ProxyError::HmdsError(error) => write!(f, "{error}"),
41            ProxyError::ImdiError(imd_interpret_error) => write!(f, "{imd_interpret_error}"),
42            ProxyError::NativeError(native_error) => write!(f, "{native_error}"),
43        }
44    }
45}
46
47pub type ImdInterpretValue = (StatusCode, http::Response<Vec<u8>>, LogMsgs);
48pub type ImdInterpretResultError = (Box<ImdInterpretError>, LogMsgs);
49pub type ImdInterpretResult = Result<ImdInterpretValue, (Box<ImdInterpretError>, LogMsgs)>;
50
51#[derive(Debug)]
52pub enum ImdInterpretError {
53    TransformError(Box<dyn std::error::Error>),
54    UpstreamError(String),
55    RuntimeError(Box<dyn std::error::Error>),
56    MetadataProcessingError(Box<Error>),
57}
58
59impl Display for ImdInterpretError {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "Interconnection Metadata Interpretation Error: ")?;
62        match self {
63            ImdInterpretError::MetadataProcessingError(ps_interpret_error) => {
64                write!(f, "{ps_interpret_error}")
65            }
66            ImdInterpretError::UpstreamError(ue) => {
67                write!(f, "{ue}")
68            }
69            ImdInterpretError::RuntimeError(re) => {
70                write!(f, "{re}")
71            }
72            ImdInterpretError::TransformError(te) => write!(f, "{te}"),
73        }
74    }
75}
76
77impl std::error::Error for ImdInterpretError {}