Skip to content

Commit e615431

Browse files
author
Michael Brewer
committed
refactor(api-gateway): rename to statusCode
1 parent 4d37480 commit e615431

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import zlib
66
from enum import Enum
7+
from http import HTTPStatus
78
from typing import Any, Callable, Dict, List, Optional, Set, Union
89

910
from aws_lambda_powertools.shared.json_encoder import Encoder
@@ -35,28 +36,28 @@ class BadRequestError(ServiceError):
3536
"""Bad Request Error"""
3637

3738
def __init__(self, msg: str):
38-
super().__init__(400, msg)
39+
super().__init__(HTTPStatus.BAD_REQUEST.value, msg)
3940

4041

4142
class UnauthorizedError(ServiceError):
4243
"""Unauthorized Error"""
4344

4445
def __init__(self, msg: str):
45-
super().__init__(401, msg)
46+
super().__init__(HTTPStatus.UNAUTHORIZED.value, msg)
4647

4748

4849
class NotFoundError(ServiceError):
4950
"""Not Found Error"""
5051

5152
def __init__(self, msg: str = "Not found"):
52-
super().__init__(404, msg)
53+
super().__init__(HTTPStatus.NOT_FOUND.value, msg)
5354

5455

5556
class InternalServerError(ServiceError):
5657
"""Internal Server Error"""
5758

5859
def __init__(self, message: str):
59-
super().__init__(500, message)
60+
super().__init__(HTTPStatus.INTERNAL_SERVER_ERROR.value, message)
6061

6162

6263
class ProxyEventType(Enum):
@@ -511,10 +512,10 @@ def _not_found(self, method: str) -> ResponseBuilder:
511512

512513
return ResponseBuilder(
513514
Response(
514-
status_code=404,
515+
status_code=HTTPStatus.NOT_FOUND.value,
515516
content_type=APPLICATION_JSON,
516517
headers=headers,
517-
body=self._json_dump({"code": 404, "message": "Not found"}),
518+
body=self._json_dump({"statusCode": HTTPStatus.NOT_FOUND.value, "message": "Not found"}),
518519
)
519520
)
520521

@@ -527,7 +528,7 @@ def _call_route(self, route: Route, args: Dict[str, str]) -> ResponseBuilder:
527528
Response(
528529
status_code=e.status_code,
529530
content_type=APPLICATION_JSON,
530-
body=self._json_dump({"code": e.status_code, "message": e.msg}),
531+
body=self._json_dump({"statusCode": e.status_code, "message": e.msg}),
531532
),
532533
route,
533534
)

tests/functional/event_handler/test_api_gateway.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def bad_request_error():
517517
# AND status code equals 400
518518
assert result["statusCode"] == 400
519519
assert result["headers"]["Content-Type"] == APPLICATION_JSON
520-
expected = {"code": 400, "message": "Missing required parameter"}
520+
expected = {"statusCode": 400, "message": "Missing required parameter"}
521521
assert result["body"] == json_dump(expected)
522522

523523
# GIVEN an UnauthorizedError
@@ -532,7 +532,7 @@ def unauthorized_error():
532532
# AND status code equals 401
533533
assert result["statusCode"] == 401
534534
assert result["headers"]["Content-Type"] == APPLICATION_JSON
535-
expected = {"code": 401, "message": "Unauthorized"}
535+
expected = {"statusCode": 401, "message": "Unauthorized"}
536536
assert result["body"] == json_dump(expected)
537537

538538
# GIVEN an NotFoundError
@@ -547,7 +547,7 @@ def not_found_error():
547547
# AND status code equals 404
548548
assert result["statusCode"] == 404
549549
assert result["headers"]["Content-Type"] == APPLICATION_JSON
550-
expected = {"code": 404, "message": "Not found"}
550+
expected = {"statusCode": 404, "message": "Not found"}
551551
assert result["body"] == json_dump(expected)
552552

553553
# GIVEN an InternalServerError
@@ -562,7 +562,7 @@ def internal_server_error():
562562
# AND status code equals 500
563563
assert result["statusCode"] == 500
564564
assert result["headers"]["Content-Type"] == APPLICATION_JSON
565-
expected = {"code": 500, "message": "Internal server error"}
565+
expected = {"statusCode": 500, "message": "Internal server error"}
566566
assert result["body"] == json_dump(expected)
567567

568568
# GIVEN an ServiceError with a custom status code
@@ -578,5 +578,5 @@ def service_error():
578578
assert result["statusCode"] == 502
579579
assert result["headers"]["Content-Type"] == APPLICATION_JSON
580580
assert "Access-Control-Allow-Origin" in result["headers"]
581-
expected = {"code": 502, "message": "Something went wrong!"}
581+
expected = {"statusCode": 502, "message": "Something went wrong!"}
582582
assert result["body"] == json_dump(expected)

0 commit comments

Comments
 (0)