From 2af9f6814723078af8a53dcb1072117c4c98e60b Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 1 Nov 2024 17:59:46 -0500 Subject: [PATCH] LogReader: more specific exceptions (#33914) * more specific logreader exceptions * huh * fix --- selfdrive/car/tests/test_models.py | 10 ++++------ tools/lib/logreader.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/selfdrive/car/tests/test_models.py b/selfdrive/car/tests/test_models.py index 025c3ab660..2a62127749 100644 --- a/selfdrive/car/tests/test_models.py +++ b/selfdrive/car/tests/test_models.py @@ -22,7 +22,7 @@ from openpilot.selfdrive.selfdrived.selfdrived import SelfdriveD from openpilot.selfdrive.pandad import can_capnp_to_list from openpilot.selfdrive.test.helpers import read_segment_list from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT -from openpilot.tools.lib.logreader import LogReader +from openpilot.tools.lib.logreader import LogReader, LogsUnavailable from openpilot.tools.lib.route import SegmentName from panda.tests.libpanda import libpanda_py @@ -113,10 +113,8 @@ class TestCarModelBase(unittest.TestCase): (SafetyModel.elm327, SafetyModel.noOutput): cls.car_safety_mode_frame = len(can_msgs) - if len(can_msgs) > int(50 / DT_CTRL): - return car_fw, can_msgs, experimental_long - - raise Exception("no can data found") + assert len(can_msgs) > int(50 / DT_CTRL), "no can data found" + return car_fw, can_msgs, experimental_long @classmethod def get_testing_data(cls): @@ -130,7 +128,7 @@ class TestCarModelBase(unittest.TestCase): try: lr = LogReader(segment_range) return cls.get_testing_data_from_logreader(lr) - except Exception: + except (LogsUnavailable, AssertionError): pass raise Exception(f"Route: {repr(cls.test_route.route)} with segments: {test_segs} not found or no CAN msgs found. Is it uploaded and public?") diff --git a/tools/lib/logreader.py b/tools/lib/logreader.py index cf8748929c..5f7cdd3043 100755 --- a/tools/lib/logreader.py +++ b/tools/lib/logreader.py @@ -49,7 +49,7 @@ class _LogFileReader: _, ext = os.path.splitext(urllib.parse.urlparse(fn).path) if ext not in ('', '.bz2', '.zst'): # old rlogs weren't compressed - raise Exception(f"unknown extension {ext}") + raise ValueError(f"unknown extension {ext}") with FileReader(fn) as f: dat = f.read() @@ -99,6 +99,10 @@ Source = Callable[[SegmentRange, ReadMode], list[LogPath]] InternalUnavailableException = Exception("Internal source not available") +class LogsUnavailable(Exception): + pass + + @cache def default_valid_file(fn: LogPath) -> bool: return fn is not None and file_exists(fn) @@ -128,7 +132,7 @@ def apply_strategy(mode: ReadMode, rlog_paths: list[LogPath], qlog_paths: list[L return auto_strategy(rlog_paths, qlog_paths, False, valid_file) elif mode == ReadMode.AUTO_INTERACTIVE: return auto_strategy(rlog_paths, qlog_paths, True, valid_file) - raise Exception(f"invalid mode: {mode}") + raise ValueError(f"invalid mode: {mode}") def comma_api_source(sr: SegmentRange, mode: ReadMode) -> list[LogPath]: @@ -224,8 +228,8 @@ def auto_source(sr: SegmentRange, mode=ReadMode.RLOG, sources: list[Source] = No except Exception as e: exceptions[source.__name__] = e - raise Exception("auto_source could not find any valid source, exceptions for sources:\n - " + - "\n - ".join([f"{k}: {repr(v)}" for k, v in exceptions.items()])) + raise LogsUnavailable("auto_source could not find any valid source, exceptions for sources:\n - " + + "\n - ".join([f"{k}: {repr(v)}" for k, v in exceptions.items()])) def parse_indirect(identifier: str) -> str: