Skip to content

Commit

Permalink
Added error loggers in quart error_handler.
Browse files Browse the repository at this point in the history
Also fixed the default quart error handler (500) printing the stacktrace. MIP-476
  • Loading branch information
ThanKarab committed Jul 17, 2023
1 parent f0cd296 commit ca6c492
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
2 changes: 0 additions & 2 deletions exareme2/controller/api/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ async def post_algorithm(algorithm_name: str) -> str:
if not algorithm_request_dto.request_id:
algorithm_request_dto.request_id = UIDGenerator().get_a_uid()

# request_id = algorithm_request_dto.request_id or UIDGenerator().get_a_uid()
controller.validate_algorithm_execution_request(
algorithm_name=algorithm_name, algorithm_request_dto=algorithm_request_dto
)

algorithm_result = await controller.exec_algorithm(
# request_id=request_id,
algorithm_name=algorithm_name,
algorithm_request_dto=algorithm_request_dto,
)
Expand Down
57 changes: 36 additions & 21 deletions exareme2/controller/api/error_handlers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import enum
import traceback

from quart import Blueprint

from exareme2.controller import controller_logger as ctrl_logger
from exareme2.controller.api.validator import BadRequest
from exareme2.controller.celery_app import CeleryTaskTimeoutException
from exareme2.controller.controller import NodeTaskTimeoutException
from exareme2.controller.controller import NodeUnresponsiveException
from exareme2.controller.controller_logger import get_background_service_logger
from exareme2.exceptions import BadUserInput
from exareme2.exceptions import DataModelUnavailable
from exareme2.exceptions import DatasetUnavailable
Expand Down Expand Up @@ -35,47 +35,67 @@ class HTTPStatusCode(enum.IntEnum):

@error_handlers.app_errorhandler(BadRequest)
def handle_bad_request(error: BadRequest):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.BAD_REQUEST


@error_handlers.app_errorhandler(FilterError)
def handle_bad_request(error: FilterError):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.BAD_REQUEST


@error_handlers.app_errorhandler(BadUserInput)
def handle_bad_user_input(error: BadUserInput):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.BAD_USER_INPUT


@error_handlers.app_errorhandler(DataModelUnavailable)
def handle_bad_user_input(error: DataModelUnavailable):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.BAD_USER_INPUT


@error_handlers.app_errorhandler(DatasetUnavailable)
def handle_bad_user_input(error: DatasetUnavailable):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.BAD_USER_INPUT


@error_handlers.app_errorhandler(InsufficientDataError)
def handle_privacy_error(error: InsufficientDataError):
# TODO: Add proper context id. Related JIRA issue: https://team-1617704806227.atlassian.net/browse/MIP-486
# ctrl_logger.get_request_logger("demoContextId123").info(
# f"Insufficient Data Error: \n " + error.message
# )
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return INSUFFICIENT_DATA_ERROR_MESSAGE, HTTPStatusCode.INSUFFICIENT_DATA_ERROR


@error_handlers.app_errorhandler(SMPCUsageError)
def handle_smpc_error(error: SMPCUsageError):
get_background_service_logger().info(
f"Request Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return error.message, HTTPStatusCode.SMPC_USAGE_ERROR


@error_handlers.app_errorhandler(NodeUnresponsiveException)
def handle_node_unresponsive_algorithm_excecution_exception(
error: NodeUnresponsiveException,
):
get_background_service_logger().error(
f"Internal Server Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return (
error.message,
HTTPStatusCode.NODE_UNRESPONSIVE_ALGORITHM_EXECUTION_ERROR,
Expand All @@ -86,24 +106,19 @@ def handle_node_unresponsive_algorithm_excecution_exception(
def handle_node_task_timeout_algorithm_execution_exception(
error: NodeTaskTimeoutException,
):
get_background_service_logger().error(
f"Internal Server Error. Type: '{type(error).__name__}' Message: '{error}'"
)
return (
error.message,
HTTPStatusCode.NODE_TASK_TIMEOUT_ALGORITHM_EXECUTION_ERROR,
)


# TODO BUG https://team-1617704806227.atlassian.net/browse/MIP-476
# Default error handler doesn't contain enough error information.
# It's better to propagate, the error it's at least visible
# @error_handlers.app_errorhandler(Exception)
# def handle_unexpected_exception(error: Exception):
# TODO: Add proper context id. Related JIRA issue: https://team-1617704806227.atlassian.net/browse/MIP-486

# ctrl_logger.getRequestLogger("demoContextId123").error(
# f"Internal Server Error."
# f"\nErrorType: {type(error)}"
# f"\nError: {error}"
# f"\nTraceback: {traceback.print_tb(error.__traceback__)}"
# )
#
# return "", HTTPStatusCode.UNEXPECTED_ERROR
@error_handlers.app_errorhandler(Exception)
def handle_unexpected_exception(error: Exception):
get_background_service_logger().error(
f"Internal Server Error. Type: '{type(error).__name__}' Message: '{error}'"
)
traceback.print_tb(error.__traceback__)
return "", HTTPStatusCode.UNEXPECTED_ERROR
2 changes: 1 addition & 1 deletion exareme2/controller/controller_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def init_logger(request_id, log_level=None):
return logger


def get_background_service_logger():
def get_background_service_logger() -> logging.Logger:
"""
Used for logging information produced by any background service.
"""
Expand Down

0 comments on commit ca6c492

Please sign in to comment.