diff options
-rw-r--r-- | bazarr/app/get_providers.py | 33 | ||||
-rw-r--r-- | tests/bazarr/test_app_get_providers.py | 15 |
2 files changed, 44 insertions, 4 deletions
diff --git a/bazarr/app/get_providers.py b/bazarr/app/get_providers.py index 98bd1365c..23839046b 100644 --- a/bazarr/app/get_providers.py +++ b/bazarr/app/get_providers.py @@ -10,6 +10,8 @@ import pretty import time import socket import requests +import traceback +import re from subliminal_patch.exceptions import TooManyRequests, APIThrottled, ParseResponseError, IPAddressBlocked, \ MustGetBlacklisted, SearchLimitReached @@ -28,6 +30,9 @@ from sonarr.blacklist import blacklist_log from utilities.analytics import event_tracker +_TRACEBACK_RE = re.compile(r'File "(.*?)", line (\d+)') + + def time_until_midnight(timezone): # type: (datetime.datetime) -> datetime.timedelta """ @@ -342,15 +347,35 @@ def provider_throttle(name, exception): tp[name] = (cls_name, throttle_until, throttle_description) set_throttled_providers(str(tp)) + trac_info = _get_traceback_info(exception) + logging.info("Throttling %s for %s, until %s, because of: %s. Exception info: %r", name, - throttle_description, throttle_until.strftime("%y/%m/%d %H:%M"), cls_name, exception.args[0] - if exception.args else None) - event_tracker.track_throttling(provider=name, exception_name=cls_name, exception_info=exception.args[0] - if exception.args else None) + throttle_description, throttle_until.strftime("%y/%m/%d %H:%M"), cls_name, trac_info) + event_tracker.track_throttling(provider=name, exception_name=cls_name, exception_info=trac_info) update_throttled_provider() +def _get_traceback_info(exc: Exception): + traceback_str = " ".join(traceback.format_tb(exc.__traceback__)) + + clean_msg = str(exc).replace("\n", " ").strip() + + line_info = _TRACEBACK_RE.search(traceback_str) + + # Value info char len is 100 + + if line_info is None: + return clean_msg[:100] + + file_, line = line_info.groups() + + extra = f"' ~ {file_}@{line}"[:90] + message = f"'{clean_msg}"[:100 - len(extra)] + + return message + extra + + def throttled_count(name): global throttle_count if name in list(throttle_count.keys()): diff --git a/tests/bazarr/test_app_get_providers.py b/tests/bazarr/test_app_get_providers.py index e6a31a39f..e8f344ef2 100644 --- a/tests/bazarr/test_app_get_providers.py +++ b/tests/bazarr/test_app_get_providers.py @@ -113,3 +113,18 @@ def test_get_language_equals_injected_settings_hi(): result = get_providers.get_language_equals(config) assert result == [(Language("eng", hi=True), Language("eng"))] + + +def _get_error(): + try: + raise ValueError("Some error" * 100) + except ValueError as error: + return error + + +def test_get_traceback_info(): + error_ = _get_error() + + if error_ is not None: + msg = get_providers._get_traceback_info(error_) + assert len(msg) == 100 |