8
8
)
9
9
10
10
11
+ class GatewayResponseCode (StatusCode , Enum ):
12
+ REQUEST_TOO_LARGE = "413"
13
+ RESOURCE_NOT_FOUND = "404"
14
+ AUTHORIZER_CONFIGURATION_ERROR = "500"
15
+ MISSING_AUTHENTICATION_TOKEN = "403"
16
+ BAD_REQUEST_BODY = "400"
17
+ INVALID_SIGNATURE = "403"
18
+ INVALID_API_KEY = "403"
19
+ BAD_REQUEST_PARAMETERS = "400"
20
+ AUTHORIZER_FAILURE = "500"
21
+ UNAUTHORIZED = "401"
22
+ INTEGRATION_TIMEOUT = "504"
23
+ ACCESS_DENIED = "403"
24
+ DEFAULT_4XX = ""
25
+ DEFAULT_5XX = ""
26
+ WAF_FILTERED = "403"
27
+ QUOTA_EXCEEDED = "429"
28
+ THROTTLED = "429"
29
+ API_CONFIGURATION_ERROR = "500"
30
+ UNSUPPORTED_MEDIA_TYPE = "415"
31
+ INTEGRATION_FAILURE = "504"
32
+ EXPIRED_TOKEN = "403"
33
+
34
+
11
35
class BaseGatewayException (Exception ):
12
36
"""
13
37
Base class for all Gateway exceptions
14
38
Do not raise from this class directly. Instead, raise the specific Exception
15
39
"""
16
40
17
41
message : str = "Unimplemented Response"
18
- type : GatewayResponseType
42
+ type : GatewayResponseType = None
19
43
status_code : int | str = None
20
44
code : str = ""
21
45
@@ -24,14 +48,17 @@ def __init__(self, message: str = None, status_code: int | str = None):
24
48
self .message = message
25
49
if status_code is not None :
26
50
self .status_code = status_code
51
+ elif self .status_code is None and self .type :
52
+ # Fallback to the default value
53
+ self .status_code = GatewayResponseCode [self .type ]
27
54
28
55
29
56
class Default4xxError (BaseGatewayException ):
30
57
"""Do not raise from this class directly.
31
58
Use one of the subclasses instead, as they contain the appropriate header
32
59
"""
33
60
34
- type : GatewayResponseType .DEFAULT_4XX
61
+ type = GatewayResponseType .DEFAULT_4XX
35
62
status_code = 400
36
63
37
64
@@ -40,7 +67,7 @@ class Default5xxError(BaseGatewayException):
40
67
Use one of the subclasses instead, as they contain the appropriate header
41
68
"""
42
69
43
- type : GatewayResponseType .DEFAULT_5XX
70
+ type = GatewayResponseType .DEFAULT_5XX
44
71
status_code = 500
45
72
46
73
@@ -98,8 +125,9 @@ class ExpiredTokenError(BaseGatewayException):
98
125
99
126
class IntegrationFailureError (BaseGatewayException ):
100
127
type = GatewayResponseType .INTEGRATION_FAILURE
101
- # TODO validate this header with aws validated tests
102
- code = "IntegrationFailureException"
128
+ # TODO: tested manually for now
129
+ code = "InternalServerErrorException"
130
+ status_code = 500
103
131
104
132
105
133
class IntegrationTimeoutError (BaseGatewayException ):
@@ -161,30 +189,6 @@ class WafFilteredError(BaseGatewayException):
161
189
code = "WafFilteredException"
162
190
163
191
164
- class GatewayResponseCode (StatusCode , Enum ):
165
- REQUEST_TOO_LARGE = "413"
166
- RESOURCE_NOT_FOUND = "404"
167
- AUTHORIZER_CONFIGURATION_ERROR = "500"
168
- MISSING_AUTHENTICATION_TOKEN = "403"
169
- BAD_REQUEST_BODY = "400"
170
- INVALID_SIGNATURE = "403"
171
- INVALID_API_KEY = "403"
172
- BAD_REQUEST_PARAMETERS = "400"
173
- AUTHORIZER_FAILURE = "500"
174
- UNAUTHORIZED = "401"
175
- INTEGRATION_TIMEOUT = "504"
176
- ACCESS_DENIED = "403"
177
- DEFAULT_4XX = ""
178
- DEFAULT_5XX = ""
179
- WAF_FILTERED = "403"
180
- QUOTA_EXCEEDED = "429"
181
- THROTTLED = "429"
182
- API_CONFIGURATION_ERROR = "500"
183
- UNSUPPORTED_MEDIA_TYPE = "415"
184
- INTEGRATION_FAILURE = "504"
185
- EXPIRED_TOKEN = "403"
186
-
187
-
188
192
def build_gateway_response (
189
193
response_type : GatewayResponseType ,
190
194
status_code : StatusCode = None ,
@@ -199,10 +203,9 @@ def build_gateway_response(
199
203
or {"application/json" : '{"message":$context.error.messageString}' },
200
204
responseType = response_type ,
201
205
defaultResponse = default_response ,
206
+ statusCode = status_code ,
202
207
)
203
- if status_code or (status_code := GatewayResponseCode [response_type ]):
204
- # DEFAULT_4XX and DEFAULT_5XX do not have `statusCode` present in the response
205
- response ["statusCode" ] = status_code
208
+
206
209
return response
207
210
208
211
0 commit comments