From a1ceee6e20163f31d6c555d6a58dfce7440f2d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20T=C3=A4nzer?= Date: Thu, 7 Dec 2023 17:24:22 +0100 Subject: [PATCH] [issue-775] catch decoding errors while parsing using the cli tool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Armin Tänzer --- src/spdx_tools/spdx/clitools/pyspdxtools.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/spdx_tools/spdx/clitools/pyspdxtools.py b/src/spdx_tools/spdx/clitools/pyspdxtools.py index 6a46b0d84..4219c6b81 100644 --- a/src/spdx_tools/spdx/clitools/pyspdxtools.py +++ b/src/spdx_tools/spdx/clitools/pyspdxtools.py @@ -14,9 +14,13 @@ # limitations under the License. import logging import sys +from json import JSONDecodeError +from xml.parsers.expat import ExpatError +from xml.sax import SAXParseException import click from beartype.typing import List +from yaml.scanner import ScannerError from spdx_tools.spdx.graph_generation import export_graph_from_document from spdx_tools.spdx.model import Document @@ -113,6 +117,22 @@ def main(infile: str, outfile: str, version: str, novalidation: bool, graph: boo logging.error(log_string) sys.exit(1) + except JSONDecodeError as err: + logging.error(f"Invalid JSON provided: {err.args[0]}") + sys.exit(1) + + except ScannerError as err: + logging.error("Invalid YAML provided: " + "\n".join([str(arg) for arg in err.args])) + sys.exit(1) + + except ExpatError as err: + logging.error(f"Invalid XML provided: {err.args[0]}") + sys.exit(1) + + except SAXParseException as err: + logging.error(f"Invalid RDF-XML provided: {str(err)}") + sys.exit(1) + except FileNotFoundError as err: logging.error(f"{err.strerror}: {err.filename}") sys.exit(1)