From b0a3a21133155c21dab92677cc42add822b40398 Mon Sep 17 00:00:00 2001 From: Alexandre Conrad-Dormoy Date: Sun, 26 May 2024 00:21:03 -0700 Subject: [PATCH] chore: Tell why a coverage report can't be parsed (#187) --- CHANGES.md | 1 + pycobertura/cobertura.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8b59ded..e87b3d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Release Notes ## Unreleased +* Improve error message as to why parsing the Cobertura report failed. ## 3.3.1 (2024-02-17) * Fix total stmts, miss and cover values in the coverage report when an ignore diff --git a/pycobertura/cobertura.py b/pycobertura/cobertura.py index 5dab495..28837ce 100644 --- a/pycobertura/cobertura.py +++ b/pycobertura/cobertura.py @@ -54,6 +54,7 @@ def __init__(self, report, filesystem=None): source files referenced in the report. Please check the `pycobertura.filesystem` module to learn more about filesystems. """ + errors = [] for load_func in [ self._load_from_file, self._load_from_string, @@ -61,10 +62,19 @@ def __init__(self, report, filesystem=None): try: self.xml = load_func(report) break - except BaseException: + except BaseException as e: + errors.append(e) pass else: - raise self.InvalidCoverageReport("Invalid coverage file: {}".format(report)) + raise self.InvalidCoverageReport( + """\ +Invalid coverage report: {}. +The following exceptions occurred while attempting to parse the report: +* While treating the report as a filename: {}. +* While treating the report as an XML Cobertura string: {}""".format( + report, errors[0], errors[1] + ) + ) self.filesystem = filesystem self.report = report