From d1a5de30abe364fdc293e69a0f9148a1110bdef3 Mon Sep 17 00:00:00 2001 From: drew2a Date: Mon, 27 Nov 2023 11:57:01 +0100 Subject: [PATCH 1/4] Add an exclusion for the exception type found in the latest core output. Specifically, it includes the word 'warning' in the list of exclusions. Refactored the parse_last_core_output function to improve readability and maintainability. Added a regular expression pattern _re_search_exception_exclusions to exclude specific exception types from being parsed. Modified the parse_last_core_output function to skip parsing if an excluded exception type is found. --- .../core/sentry_reporter/sentry_tools.py | 7 ++++++- .../tests/test_sentry_tools.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index 5842533dc8..eb2746ae7e 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -12,6 +12,7 @@ # Find an exception in the string like: "OverflowError: bind(): port must be 0-65535" _re_search_exception = re.compile(r'^(\S+)\s*:\s*(.+)') +_re_search_exception_exclusions = re.compile(r'(?:warning)', re.RegexFlag.IGNORECASE) # Remove the substring like "Sentry is attempting to send 1 pending error messages" _re_remove_sentry = re.compile(r'Sentry is attempting.*') @@ -89,7 +90,11 @@ def _clean_up(s: str): for line in reversed(text.split('\n')): if m := _re_search_exception.match(line): - return LastCoreException(type=_clean_up(m.group(1)), + exception_type = m.group(1) + if _re_search_exception_exclusions.search(exception_type): + continue # find an exclusion + + return LastCoreException(type=_clean_up(exception_type), message=_clean_up(m.group(2))) return None diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py index 19bf1c75c8..6613a0d38a 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py @@ -1,7 +1,7 @@ import pytest from tribler.core.sentry_reporter.sentry_tools import ( - _re_search_exception, delete_item, + _re_search_exception, _re_search_exception_exclusions, delete_item, distinct_by, extract_dict, format_version, @@ -180,6 +180,18 @@ def test_parse_last_core_output_re(given, expected): assert m == expected +EXCEPTION_EXCLUSIONS_STRINGS = [ + 'UserWarning', +] + + +@pytest.mark.parametrize('given', EXCEPTION_EXCLUSIONS_STRINGS) +def test_re_search_exception_exclusions(given): + # Test that `_re_search_exception_exclusions` matches with the expected values from the + # `EXCEPTION_EXCLUSIONS_STRINGS` + assert _re_search_exception_exclusions.search(given) + + def test_parse_last_core_output(): # Test that `parse_last_core_output` correctly extract the last core exception from the real raw core output @@ -211,3 +223,8 @@ def test_parse_last_core_output_no_match(): last_core_exception = parse_last_core_output('last core output without exceptions') assert not last_core_exception + + +def test_parse_last_core_output_exclusion(): + last_core_exception = parse_last_core_output('UserWarning: You are using cryptography on a 32-bit Python on a...') + assert not last_core_exception From 5b406064a3ce7b7babc3e3225a36658ef9138305 Mon Sep 17 00:00:00 2001 From: drew2a Date: Mon, 27 Nov 2023 13:10:42 +0100 Subject: [PATCH 2/4] Fix regex pattern in sentry_tools.py and add new test cases to test_sentry_tools.py - Fix the regex pattern in `_re_search_exception` to properly match exception strings. - Add new test cases to `test_sentry_tools.py` for handling additional exception strings. This commit improves the accuracy of matching exception strings and expands the test coverage for handling different types of exceptions. --- src/tribler/core/sentry_reporter/sentry_tools.py | 2 +- src/tribler/core/sentry_reporter/tests/test_sentry_tools.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index eb2746ae7e..01882a000b 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -11,7 +11,7 @@ CONTEXT_DELIMITER = '--CONTEXT--' # Find an exception in the string like: "OverflowError: bind(): port must be 0-65535" -_re_search_exception = re.compile(r'^(\S+)\s*:\s*(.+)') +_re_search_exception = re.compile(r'^(\S+)\s*:\s+(.+)') _re_search_exception_exclusions = re.compile(r'(?:warning)', re.RegexFlag.IGNORECASE) # Remove the substring like "Sentry is attempting to send 1 pending error messages" diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py index 6613a0d38a..4c0b7be2d5 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py @@ -159,7 +159,8 @@ def test_extract_dict(): ('pony.orm.core.TransactionIntegrityError', "MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE"), ), - ('ERROR ', None) + ('ERROR ', None), + ('PyInstaller\loader\pyimod03_importers.py:495', None), ] From 491749c0393a20aeaa5952ab3298584f95dd0061 Mon Sep 17 00:00:00 2001 From: drew2a Date: Mon, 27 Nov 2023 13:18:41 +0100 Subject: [PATCH 3/4] Fix exception parsing in sentry_tools.py and update test cases The regular expression for finding exceptions in the string has been fixed to correctly capture the exception name and message. Additionally, the exclusion pattern for warnings has been updated. The test cases have also been modified to reflect these changes. --- src/tribler/core/sentry_reporter/sentry_tools.py | 2 +- src/tribler/core/sentry_reporter/tests/test_sentry_tools.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index 01882a000b..f8811447fc 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -11,7 +11,7 @@ CONTEXT_DELIMITER = '--CONTEXT--' # Find an exception in the string like: "OverflowError: bind(): port must be 0-65535" -_re_search_exception = re.compile(r'^(\S+)\s*:\s+(.+)') +_re_search_exception = re.compile(r'^(\S+):\s+(.+)') _re_search_exception_exclusions = re.compile(r'(?:warning)', re.RegexFlag.IGNORECASE) # Remove the substring like "Sentry is attempting to send 1 pending error messages" diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py index 4c0b7be2d5..84d3a8a221 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py @@ -155,7 +155,7 @@ def test_extract_dict(): ('OverflowError', 'bind(): port must be 0-65535'), ), - ("pony.orm.core.TransactionIntegrityError : MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE", + ("pony.orm.core.TransactionIntegrityError: MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE", ('pony.orm.core.TransactionIntegrityError', "MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE"), ), From c02ded0a7fad644d5cbfd0f2624439ca60f9283f Mon Sep 17 00:00:00 2001 From: drew2a Date: Mon, 27 Nov 2023 13:25:10 +0100 Subject: [PATCH 4/4] Fix regex pattern to match exception names with alphanumeric characters and underscores in sentry_tools.py The regular expression pattern used to find exceptions in strings has been updated to include alphanumeric characters and underscores. This ensures that exceptions with names like "pony_orm.core.TransactionIntegrityError" are correctly matched. Also, added test cases for strings that should not be matched in test_sentry_tools.py. --- .../core/sentry_reporter/sentry_tools.py | 2 +- .../tests/test_sentry_tools.py | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index f8811447fc..1cbdf8ebe5 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -11,7 +11,7 @@ CONTEXT_DELIMITER = '--CONTEXT--' # Find an exception in the string like: "OverflowError: bind(): port must be 0-65535" -_re_search_exception = re.compile(r'^(\S+):\s+(.+)') +_re_search_exception = re.compile(r'^([A-Za-z0-9_.]+):\s+(.+)') _re_search_exception_exclusions = re.compile(r'(?:warning)', re.RegexFlag.IGNORECASE) # Remove the substring like "Sentry is attempting to send 1 pending error messages" diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py index 84d3a8a221..4812a157dd 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py @@ -151,16 +151,27 @@ def test_extract_dict(): ] EXCEPTION_STRINGS = [ - ('OverflowError: bind(): port must be 0-65535', - ('OverflowError', 'bind(): port must be 0-65535'), - ), - - ("pony.orm.core.TransactionIntegrityError: MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE", - ('pony.orm.core.TransactionIntegrityError', "MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE"), - ), - + ( + 'OverflowError: bind(): port must be 0-65535', + ( + 'OverflowError', + 'bind(): port must be 0-65535' + ), + ), + + ( + "pony_orm.core.TransactionIntegrityError: MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE", + ( + 'pony_orm.core.TransactionIntegrityError', + "MiscData['db_version'] cannot be stored. IntegrityError: UNIQUE" + ), + ), + + # Strings thst should not be matched ('ERROR ', None), ('PyInstaller\loader\pyimod03_importers.py:495', None), + ('foo:bar: baz', None), + ('foo: baz', None), ]