Skip to content
Snippets Groups Projects
Commit 2566d6c9 authored by Simon Künzel's avatar Simon Künzel
Browse files

Better client error logging

parent f2bd3a02
No related branches found
No related tags found
No related merge requests found
Pipeline #7253 failed
Pipeline: development

#7254

    ......@@ -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"
    ......
    import os
    import tempfile
    import atexit
    DEBUG = True
    LOG_CLIENT_ERRORS_WHEN_DEBUGGING = False
    DISABLE_SCHEDULER = True
    API_ROULETTE_MODE = 0
    ......
    ......@@ -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)}':")
    ......
    Subproject commit 6c08b477832da79999473a3ae26c4162684c75f2
    Subproject commit abf6b47372735f79050479c7e222689df213571f
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment