[refactor] change two error response http status
Browse files- src/app.py +8 -6
src/app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""Lambda entry point"""
|
2 |
import time
|
3 |
from http import HTTPStatus
|
|
|
4 |
|
5 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
6 |
from pydantic import ValidationError
|
@@ -10,16 +11,17 @@ from src.io.lambda_helpers import get_parsed_request_body, get_parsed_bbox_point
|
|
10 |
from src.prediction_api.predictors import samexporter_predict
|
11 |
|
12 |
|
13 |
-
def lambda_handler(event:
|
14 |
"""
|
15 |
-
Handle the request for the serverless backend
|
|
|
16 |
|
17 |
Args:
|
18 |
event: request content
|
19 |
context: request context
|
20 |
|
21 |
Returns:
|
22 |
-
|
23 |
|
24 |
"""
|
25 |
app_logger.info(f"start with aws_request_id:{context.aws_request_id}.")
|
@@ -33,21 +35,21 @@ def lambda_handler(event: dict, context: LambdaContext) -> str:
|
|
33 |
request_input = get_parsed_request_body(event)
|
34 |
app_logger.info("event parsed: ok")
|
35 |
body_request = get_parsed_bbox_points(request_input)
|
|
|
36 |
|
37 |
try:
|
38 |
-
app_logger.info(f"body_request => {type(body_request)}, {body_request}.")
|
39 |
body_response = samexporter_predict(body_request["bbox"], body_request["prompt"], body_request["zoom"])
|
40 |
app_logger.info(f"output body_response:{body_response}.")
|
41 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
42 |
except Exception as ex2:
|
43 |
app_logger.error(f"exception2:{ex2}.")
|
44 |
-
response = get_response(HTTPStatus.
|
45 |
except ValidationError as va1:
|
46 |
app_logger.error(f"ValidationError:{va1}.")
|
47 |
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id, {})
|
48 |
except Exception as ex1:
|
49 |
app_logger.error(f"exception1:{ex1}.")
|
50 |
-
response = get_response(HTTPStatus.
|
51 |
|
52 |
app_logger.info(f"response_dumped:{response}...")
|
53 |
return response
|
|
|
1 |
"""Lambda entry point"""
|
2 |
import time
|
3 |
from http import HTTPStatus
|
4 |
+
from typing import Dict
|
5 |
|
6 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
7 |
from pydantic import ValidationError
|
|
|
11 |
from src.prediction_api.predictors import samexporter_predict
|
12 |
|
13 |
|
14 |
+
def lambda_handler(event: Dict, context: LambdaContext) -> str:
|
15 |
"""
|
16 |
+
Handle the request for the serverless backend and return the response
|
17 |
+
(success or a type of error based on the exception raised).
|
18 |
|
19 |
Args:
|
20 |
event: request content
|
21 |
context: request context
|
22 |
|
23 |
Returns:
|
24 |
+
json response from get_response() function
|
25 |
|
26 |
"""
|
27 |
app_logger.info(f"start with aws_request_id:{context.aws_request_id}.")
|
|
|
35 |
request_input = get_parsed_request_body(event)
|
36 |
app_logger.info("event parsed: ok")
|
37 |
body_request = get_parsed_bbox_points(request_input)
|
38 |
+
app_logger.info(f"body_request => {type(body_request)}, {body_request}.")
|
39 |
|
40 |
try:
|
|
|
41 |
body_response = samexporter_predict(body_request["bbox"], body_request["prompt"], body_request["zoom"])
|
42 |
app_logger.info(f"output body_response:{body_response}.")
|
43 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
44 |
except Exception as ex2:
|
45 |
app_logger.error(f"exception2:{ex2}.")
|
46 |
+
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id, {})
|
47 |
except ValidationError as va1:
|
48 |
app_logger.error(f"ValidationError:{va1}.")
|
49 |
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id, {})
|
50 |
except Exception as ex1:
|
51 |
app_logger.error(f"exception1:{ex1}.")
|
52 |
+
response = get_response(HTTPStatus.BAD_REQUEST.value, start_time, context.aws_request_id, {})
|
53 |
|
54 |
app_logger.info(f"response_dumped:{response}...")
|
55 |
return response
|