diff --git a/config/api_example_config.py b/config/api_example_config.py index dbf8a4b05a41587e6191f0b06c0c56beda2b8320..b30711d18db37122e08ad14a4b5af8204fb95c5c 100644 --- a/config/api_example_config.py +++ b/config/api_example_config.py @@ -17,6 +17,9 @@ PERMANENT_MEDIA_DIR = "permanent_media" # Defaults for development, do not use in production! DEBUG = True +# Logs client errors when HTTP request fails (usually because of a client's mistake, e.g. bad request) +LOG_CLIENT_ERRORS_WHEN_DEBUGGING = True + # Name of this instance API_SERVER_NAME = "dev" diff --git a/config/test_config_override.py b/config/test_config_override.py index 8f699b2cd7d6b8ea71167dd4fa1ad3d8fe9cf622..a00bca0a4d5a1ba5f97bb9d4555a2fa982309eba 100644 --- a/config/test_config_override.py +++ b/config/test_config_override.py @@ -1,8 +1,5 @@ -import os -import tempfile -import atexit - DEBUG = True +LOG_CLIENT_ERRORS_WHEN_DEBUGGING = False DISABLE_SCHEDULER = True API_ROULETTE_MODE = 0 diff --git a/src/api/routes/route.py b/src/api/routes/route.py index dacc9584205c3ebc63efc8c12955fbcd082c2616..faf31179d30773d6e558ce0de17c260d9d117b7a 100644 --- a/src/api/routes/route.py +++ b/src/api/routes/route.py @@ -20,6 +20,9 @@ _DEFAULT_CACHE_CONTROL_MAX_AGE_SECONDS = api.config["DEFAULT_CACHE_CONTROL_MAX_A _SERVER_NAME = api.config["API_SERVER_NAME"] +_LOG_CLIENT_ERRORS: bool = DEBUG_ENABLED and api.config.get("LOG_CLIENT_ERRORS_WHEN_DEBUGGING", True) + + def get_client_json(request: Request) -> CJsonObject: json = request.get_json(force=False, silent=True, cache=False) if json is None: @@ -86,24 +89,24 @@ def api_request_get_query_boolean(id: str, default: bool or None) -> int or None @api.app.errorhandler(400) def _handle_bad_request(e=None): - if DEBUG_ENABLED and e is not None: - print(f"An exception occurred while handling api request '{request.path}':") + if _LOG_CLIENT_ERRORS and e is not None: + print(f"A client exception occurred while handling api request '{request.path}':") traceback.print_exception(e) return api_on_error(ERROR_BAD_REQUEST("Bad request")) @api.app.errorhandler(404) def _handle_not_found(e=None): - if DEBUG_ENABLED and e is not None: - print(f"An exception occurred while handling api request '{request.path}':") + if _LOG_CLIENT_ERRORS and e is not None: + print(f"A client exception occurred while handling api request '{request.path}':") traceback.print_exception(e) return api_on_error(ERROR_UNKNOWN_REQUEST_PATH) @api.app.errorhandler(405) def _handle_method_not_allowed(e=None): - if DEBUG_ENABLED and e is not None: - print(f"An exception occurred while handling api request '{request.path}':") + if _LOG_CLIENT_ERRORS and e is not None: + print(f"A client exception occurred while handling api request '{request.path}':") traceback.print_exception(e) return api_on_error(ERROR_METHOD_NOT_ALLOWED) @@ -389,8 +392,14 @@ def api_function(track_in_diagnostics: bool = True, else: # pragma: no cover raise Exception(f"Api route {truncate_string(request.path)} returned result of unknown type: {str(result)}") except CJsonException as e: + if _LOG_CLIENT_ERRORS: + print(f"A client exception occurred while handling api request '{request.path}':") + traceback.print_exception(e) return api_on_error(ERROR_REQUEST_INVALID_PARAMETER(e.path, e.message)) except ApiClientException as e: + if _LOG_CLIENT_ERRORS: + print(f"A client exception occurred while handling api request '{request.path}':") + traceback.print_exception(e) return api_on_error(e.error) except TransactionConflictError as e: print(f"An transaction conflict occurred while handling api request '{truncate_string(request.path, 200)}':") diff --git a/submodules/backend_common_py b/submodules/backend_common_py index 6c08b477832da79999473a3ae26c4162684c75f2..abf6b47372735f79050479c7e222689df213571f 160000 --- a/submodules/backend_common_py +++ b/submodules/backend_common_py @@ -1 +1 @@ -Subproject commit 6c08b477832da79999473a3ae26c4162684c75f2 +Subproject commit abf6b47372735f79050479c7e222689df213571f