Skip to main content

rustack_apigatewayv2_model/
error.rs

1//! Auto-generated from AWS ApiGatewayV2 Smithy model. DO NOT EDIT.
2//!
3//! ApiGatewayV2 errors use JSON format with a `__type` field containing the
4//! short error type name (e.g., `ResourceNotFoundException`).
5
6use std::fmt;
7
8/// Well-known ApiGatewayV2 error codes.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum ApiGatewayV2ErrorCode {
12    /// AccessDeniedException error.
13    #[default]
14    AccessDeniedException,
15    /// BadRequestException error.
16    BadRequestException,
17    /// ConflictException error.
18    ConflictException,
19    /// NotFoundException error.
20    NotFoundException,
21    /// TooManyRequestsException error.
22    TooManyRequestsException,
23    /// UnknownOperation error.
24    UnknownOperation,
25}
26
27impl ApiGatewayV2ErrorCode {
28    /// Returns the short error type string for the JSON `__type` field.
29    #[must_use]
30    pub fn error_type(&self) -> &'static str {
31        self.as_str()
32    }
33
34    /// Returns the short error code string.
35    #[must_use]
36    pub fn as_str(&self) -> &'static str {
37        match self {
38            Self::AccessDeniedException => "AccessDeniedException",
39            Self::BadRequestException => "BadRequestException",
40            Self::ConflictException => "ConflictException",
41            Self::NotFoundException => "NotFoundException",
42            Self::TooManyRequestsException => "TooManyRequestsException",
43            Self::UnknownOperation => "UnknownOperation",
44        }
45    }
46
47    /// Returns the default HTTP status code for this error.
48    #[must_use]
49    pub fn default_status_code(&self) -> http::StatusCode {
50        match self {
51            Self::BadRequestException => http::StatusCode::BAD_REQUEST,
52            Self::AccessDeniedException => http::StatusCode::FORBIDDEN,
53            Self::NotFoundException => http::StatusCode::NOT_FOUND,
54            Self::ConflictException => http::StatusCode::CONFLICT,
55            Self::TooManyRequestsException | Self::UnknownOperation => {
56                http::StatusCode::INTERNAL_SERVER_ERROR
57            }
58        }
59    }
60}
61
62impl fmt::Display for ApiGatewayV2ErrorCode {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        f.write_str(self.as_str())
65    }
66}
67
68/// An ApiGatewayV2 error response.
69#[derive(Debug)]
70pub struct ApiGatewayV2Error {
71    /// The error code.
72    pub code: ApiGatewayV2ErrorCode,
73    /// A human-readable error message.
74    pub message: String,
75    /// The HTTP status code.
76    pub status_code: http::StatusCode,
77    /// The underlying source error, if any.
78    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
79}
80
81impl fmt::Display for ApiGatewayV2Error {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(f, "ApiGatewayV2Error({}): {}", self.code, self.message)
84    }
85}
86
87impl std::error::Error for ApiGatewayV2Error {
88    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
89        self.source
90            .as_ref()
91            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
92    }
93}
94
95impl ApiGatewayV2Error {
96    /// Create a new `ApiGatewayV2Error` from an error code.
97    #[must_use]
98    pub fn new(code: ApiGatewayV2ErrorCode) -> Self {
99        Self {
100            status_code: code.default_status_code(),
101            message: code.as_str().to_owned(),
102            code,
103            source: None,
104        }
105    }
106
107    /// Create a new `ApiGatewayV2Error` with a custom message.
108    #[must_use]
109    pub fn with_message(code: ApiGatewayV2ErrorCode, message: impl Into<String>) -> Self {
110        Self {
111            status_code: code.default_status_code(),
112            message: message.into(),
113            code,
114            source: None,
115        }
116    }
117
118    /// Returns the `__type` string for the JSON error response.
119    #[must_use]
120    pub fn error_type(&self) -> &'static str {
121        self.code.error_type()
122    }
123
124    /// Internal error.
125    #[must_use]
126    pub fn internal_error(message: impl Into<String>) -> Self {
127        Self::with_message(ApiGatewayV2ErrorCode::UnknownOperation, message)
128    }
129
130    /// Unknown operation error.
131    #[must_use]
132    pub fn unknown_operation(method: &http::Method, path: &str) -> Self {
133        Self::with_message(
134            ApiGatewayV2ErrorCode::UnknownOperation,
135            format!("no ApiGatewayV2 operation matches {method} {path}"),
136        )
137    }
138
139    /// Not implemented.
140    #[must_use]
141    pub fn not_implemented(operation: &str) -> Self {
142        Self::with_message(
143            ApiGatewayV2ErrorCode::UnknownOperation,
144            format!("Operation {operation} is not yet implemented"),
145        )
146    }
147}
148
149/// Create an `ApiGatewayV2Error` from an error code.
150///
151/// # Examples
152///
153/// ```ignore
154/// let err = apigatewayv2_error!(AccessDeniedException);
155/// assert_eq!(err.code, ApiGatewayV2ErrorCode::AccessDeniedException);
156/// ```
157#[macro_export]
158macro_rules! apigatewayv2_error {
159    ($code:ident) => {
160        $crate::error::ApiGatewayV2Error::new($crate::error::ApiGatewayV2ErrorCode::$code)
161    };
162    ($code:ident, $msg:expr) => {
163        $crate::error::ApiGatewayV2Error::with_message(
164            $crate::error::ApiGatewayV2ErrorCode::$code,
165            $msg,
166        )
167    };
168}