[refactor] refactor error responses
Browse files- src/app.py +13 -21
- src/utilities/constants.py +5 -0
src/app.py
CHANGED
|
@@ -2,22 +2,21 @@ import json
|
|
| 2 |
import time
|
| 3 |
from http import HTTPStatus
|
| 4 |
from aws_lambda_powertools import Logger
|
| 5 |
-
from aws_lambda_powertools.event_handler import content_types, Response
|
| 6 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 7 |
from pydantic import BaseModel, ValidationError
|
| 8 |
|
|
|
|
| 9 |
from src.utilities.type_hints import input_floatlist, input_floatlist2
|
| 10 |
from src.utilities.utilities import base64_decode
|
| 11 |
|
|
|
|
| 12 |
logger = Logger()
|
| 13 |
|
| 14 |
|
| 15 |
class BBoxWithPointInput(BaseModel):
|
| 16 |
bbox: input_floatlist
|
| 17 |
points: input_floatlist2
|
| 18 |
-
duration_run: float = 0
|
| 19 |
message: str = ""
|
| 20 |
-
request_id: str = ""
|
| 21 |
|
| 22 |
|
| 23 |
def get_response(status: int, start_time: float, request_id: str, output: BBoxWithPointInput = None) -> str:
|
|
@@ -34,23 +33,16 @@ def get_response(status: int, start_time: float, request_id: str, output: BBoxWi
|
|
| 34 |
dict: response
|
| 35 |
|
| 36 |
"""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
output.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
response = {
|
| 45 |
"statusCode": status,
|
| 46 |
-
"
|
| 47 |
-
|
| 48 |
-
},
|
| 49 |
-
"body": body,
|
| 50 |
-
"isBase64Encoded": False
|
| 51 |
-
}
|
| 52 |
-
logger.info(f"response type:{type(response)} => {response}.")
|
| 53 |
-
return json.dumps(response)
|
| 54 |
|
| 55 |
|
| 56 |
def lambda_handler(event: dict, context: LambdaContext):
|
|
@@ -85,10 +77,10 @@ def lambda_handler(event: dict, context: LambdaContext):
|
|
| 85 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, bbox_points)
|
| 86 |
except ValidationError as ve:
|
| 87 |
logger.error(f"validation error:{ve}.")
|
| 88 |
-
response = get_response(
|
| 89 |
except Exception as e:
|
| 90 |
logger.error(f"exception:{e}.")
|
| 91 |
-
response = get_response(
|
| 92 |
|
| 93 |
logger.info(f"response_dumped:{response}...")
|
| 94 |
return response
|
|
|
|
| 2 |
import time
|
| 3 |
from http import HTTPStatus
|
| 4 |
from aws_lambda_powertools import Logger
|
|
|
|
| 5 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 6 |
from pydantic import BaseModel, ValidationError
|
| 7 |
|
| 8 |
+
from src.utilities.constants import CUSTOM_RESPONSE_MESSAGES
|
| 9 |
from src.utilities.type_hints import input_floatlist, input_floatlist2
|
| 10 |
from src.utilities.utilities import base64_decode
|
| 11 |
|
| 12 |
+
|
| 13 |
logger = Logger()
|
| 14 |
|
| 15 |
|
| 16 |
class BBoxWithPointInput(BaseModel):
|
| 17 |
bbox: input_floatlist
|
| 18 |
points: input_floatlist2
|
|
|
|
| 19 |
message: str = ""
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def get_response(status: int, start_time: float, request_id: str, output: BBoxWithPointInput = None) -> str:
|
|
|
|
| 33 |
dict: response
|
| 34 |
|
| 35 |
"""
|
| 36 |
+
duration_run = time.time() - start_time
|
| 37 |
+
if output and status == 200:
|
| 38 |
+
output.message = f"{CUSTOM_RESPONSE_MESSAGES[status]} - duration_run: {duration_run}, request_id: {request_id}."
|
| 39 |
+
return output.model_dump_json()
|
| 40 |
+
elif status == 200:
|
| 41 |
+
raise KeyError("missing BBoxWithPointInput...")
|
| 42 |
+
return json.dumps({
|
|
|
|
| 43 |
"statusCode": status,
|
| 44 |
+
"message": f"{CUSTOM_RESPONSE_MESSAGES[status]} - duration_run: {duration_run}, request_id: {request_id}."
|
| 45 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def lambda_handler(event: dict, context: LambdaContext):
|
|
|
|
| 77 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, bbox_points)
|
| 78 |
except ValidationError as ve:
|
| 79 |
logger.error(f"validation error:{ve}.")
|
| 80 |
+
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id)
|
| 81 |
except Exception as e:
|
| 82 |
logger.error(f"exception:{e}.")
|
| 83 |
+
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id)
|
| 84 |
|
| 85 |
logger.info(f"response_dumped:{response}...")
|
| 86 |
return response
|
src/utilities/constants.py
CHANGED
|
@@ -17,3 +17,8 @@ GEOJSON_SQUARE_TEMPLATE = {
|
|
| 17 |
'crs': {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}},
|
| 18 |
'features': FEATURE_SQUARE_TEMPLATE
|
| 19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
'crs': {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}},
|
| 18 |
'features': FEATURE_SQUARE_TEMPLATE
|
| 19 |
}
|
| 20 |
+
CUSTOM_RESPONSE_MESSAGES = {
|
| 21 |
+
200: "ok",
|
| 22 |
+
422: "Missing required parameter",
|
| 23 |
+
500: "Internal server error"
|
| 24 |
+
}
|