Skip to main content

aws_smithy_runtime_api/http/
error.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Error types for HTTP requests/responses.
7
8use crate::box_error::BoxError;
9use http_1x::header::{InvalidHeaderName, InvalidHeaderValue};
10use http_1x::uri::InvalidUri;
11use std::error::Error;
12use std::fmt::{Debug, Display, Formatter};
13use std::str::Utf8Error;
14
15#[derive(Debug)]
16/// An error occurred constructing an Http Request.
17///
18/// This is normally due to configuration issues, internal SDK bugs, or other user error.
19pub struct HttpError {
20    kind: Kind,
21    source: Option<BoxError>,
22}
23
24#[derive(Debug)]
25enum Kind {
26    #[cfg(feature = "http-02x")]
27    InvalidExtensions,
28    InvalidHeaderName,
29    InvalidHeaderValue,
30    InvalidMethod,
31    InvalidStatusCode,
32    InvalidUri,
33    InvalidUriParts,
34    MissingAuthority,
35    MissingScheme,
36    NonUtf8Header(NonUtf8Header),
37}
38
39#[derive(Debug)]
40pub(super) struct NonUtf8Header {
41    error: Utf8Error,
42    value: Vec<u8>,
43}
44
45impl NonUtf8Header {
46    pub(super) fn new(value: Vec<u8>, error: Utf8Error) -> Self {
47        Self { error, value }
48    }
49}
50
51impl HttpError {
52    #[cfg(feature = "http-02x")]
53    pub(super) fn invalid_extensions() -> Self {
54        Self {
55            kind: Kind::InvalidExtensions,
56            source: None,
57        }
58    }
59
60    pub(super) fn invalid_header_name(err: InvalidHeaderName) -> Self {
61        Self {
62            kind: Kind::InvalidHeaderName,
63            source: Some(Box::new(err)),
64        }
65    }
66
67    pub(super) fn invalid_method(err: http_1x::method::InvalidMethod) -> Self {
68        Self {
69            kind: Kind::InvalidMethod,
70            source: Some(Box::new(err)),
71        }
72    }
73
74    pub(super) fn invalid_header_value(err: InvalidHeaderValue) -> Self {
75        Self {
76            kind: Kind::InvalidHeaderValue,
77            source: Some(Box::new(err)),
78        }
79    }
80
81    pub(super) fn invalid_status_code() -> Self {
82        Self {
83            kind: Kind::InvalidStatusCode,
84            source: None,
85        }
86    }
87
88    pub(super) fn invalid_uri(err: InvalidUri) -> Self {
89        Self {
90            kind: Kind::InvalidUri,
91            source: Some(Box::new(err)),
92        }
93    }
94
95    #[cfg(feature = "http-02x")]
96    pub(super) fn invalid_uri_h0(err: http_02x::uri::InvalidUri) -> Self {
97        Self {
98            kind: Kind::InvalidUri,
99            source: Some(Box::new(err)),
100        }
101    }
102
103    pub(super) fn invalid_uri_parts(err: http_1x::Error) -> Self {
104        Self {
105            kind: Kind::InvalidUriParts,
106            source: Some(Box::new(err)),
107        }
108    }
109
110    pub(super) fn missing_authority() -> Self {
111        Self {
112            kind: Kind::MissingAuthority,
113            source: None,
114        }
115    }
116
117    pub(super) fn missing_scheme() -> Self {
118        Self {
119            kind: Kind::MissingScheme,
120            source: None,
121        }
122    }
123
124    pub(super) fn non_utf8_header(non_utf8_header: NonUtf8Header) -> Self {
125        Self {
126            kind: Kind::NonUtf8Header(non_utf8_header),
127            source: None,
128        }
129    }
130}
131
132impl Display for HttpError {
133    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
134        use Kind::*;
135        match &self.kind {
136            #[cfg(feature = "http-02x")]
137            InvalidExtensions => write!(f, "Extensions were provided during initialization. This prevents the request format from being converted."),
138            InvalidHeaderName => write!(f, "invalid header name"),
139            InvalidHeaderValue => write!(f, "invalid header value"),
140            InvalidMethod => write!(f, "invalid HTTP method"),
141            InvalidStatusCode => write!(f, "invalid HTTP status code"),
142            InvalidUri => write!(f, "endpoint is not a valid URI"),
143            InvalidUriParts => write!(f, "endpoint parts are not valid"),
144            MissingAuthority => write!(f, "endpoint must contain authority"),
145            MissingScheme => write!(f, "endpoint must contain scheme"),
146            NonUtf8Header(hv) => {
147                let value = String::from_utf8_lossy(&hv.value);
148                let index = hv.error.valid_up_to();
149                write!(f, "header value `{value}` contains non-UTF8 octet at index {index}")
150            },
151        }
152    }
153}
154
155impl Error for HttpError {
156    fn source(&self) -> Option<&(dyn Error + 'static)> {
157        self.source.as_ref().map(|err| err.as_ref() as _)
158    }
159}