aws_sigv4/http_request/
error.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use http0::header::{InvalidHeaderName, InvalidHeaderValue};
7use http0::uri::InvalidUri;
8use std::error::Error;
9use std::fmt;
10
11#[derive(Debug)]
12enum SigningErrorKind {
13    FailedToCreateCanonicalRequest { source: CanonicalRequestError },
14    UnsupportedIdentityType,
15}
16
17/// Error signing request
18#[derive(Debug)]
19pub struct SigningError {
20    kind: SigningErrorKind,
21}
22
23impl SigningError {
24    pub(crate) fn unsupported_identity_type() -> Self {
25        Self {
26            kind: SigningErrorKind::UnsupportedIdentityType,
27        }
28    }
29}
30
31impl fmt::Display for SigningError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self.kind {
34            SigningErrorKind::FailedToCreateCanonicalRequest { .. } => {
35                write!(f, "failed to create canonical request")
36            }
37            SigningErrorKind::UnsupportedIdentityType => {
38                write!(f, "only 'AWS credentials' are supported for signing")
39            }
40        }
41    }
42}
43
44impl Error for SigningError {
45    fn source(&self) -> Option<&(dyn Error + 'static)> {
46        match &self.kind {
47            SigningErrorKind::FailedToCreateCanonicalRequest { source } => Some(source),
48            SigningErrorKind::UnsupportedIdentityType => None,
49        }
50    }
51}
52
53impl From<CanonicalRequestError> for SigningError {
54    fn from(source: CanonicalRequestError) -> Self {
55        Self {
56            kind: SigningErrorKind::FailedToCreateCanonicalRequest { source },
57        }
58    }
59}
60
61#[derive(Debug)]
62enum CanonicalRequestErrorKind {
63    InvalidHeaderName { source: InvalidHeaderName },
64    InvalidHeaderValue { source: InvalidHeaderValue },
65    InvalidUri { source: InvalidUri },
66    UnsupportedIdentityType,
67}
68
69#[derive(Debug)]
70pub(crate) struct CanonicalRequestError {
71    kind: CanonicalRequestErrorKind,
72}
73
74impl fmt::Display for CanonicalRequestError {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        use CanonicalRequestErrorKind::*;
77        match self.kind {
78            InvalidHeaderName { .. } => write!(f, "invalid header name"),
79            InvalidHeaderValue { .. } => write!(f, "invalid header value"),
80            InvalidUri { .. } => write!(f, "the uri was invalid"),
81            UnsupportedIdentityType => {
82                write!(f, "only AWS credentials are supported for signing")
83            }
84        }
85    }
86}
87
88impl Error for CanonicalRequestError {
89    fn source(&self) -> Option<&(dyn Error + 'static)> {
90        use CanonicalRequestErrorKind::*;
91        match &self.kind {
92            InvalidHeaderName { source } => Some(source),
93            InvalidHeaderValue { source } => Some(source),
94            InvalidUri { source } => Some(source),
95            UnsupportedIdentityType => None,
96        }
97    }
98}
99
100impl CanonicalRequestError {
101    pub(crate) fn unsupported_identity_type() -> Self {
102        Self {
103            kind: CanonicalRequestErrorKind::UnsupportedIdentityType,
104        }
105    }
106}
107
108impl From<InvalidHeaderName> for CanonicalRequestError {
109    fn from(source: InvalidHeaderName) -> Self {
110        Self {
111            kind: CanonicalRequestErrorKind::InvalidHeaderName { source },
112        }
113    }
114}
115
116impl From<InvalidHeaderValue> for CanonicalRequestError {
117    fn from(source: InvalidHeaderValue) -> Self {
118        Self {
119            kind: CanonicalRequestErrorKind::InvalidHeaderValue { source },
120        }
121    }
122}
123
124impl From<InvalidUri> for CanonicalRequestError {
125    fn from(source: InvalidUri) -> Self {
126        Self {
127            kind: CanonicalRequestErrorKind::InvalidUri { source },
128        }
129    }
130}