From 97117d6b8a192821c50016b88b162d4512b8db6f Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 5 Sep 2024 11:26:31 +0200 Subject: [PATCH] improve error messages for broken 'list --graph' arguments --- conan/api/model.py | 9 +++++++-- test/integration/command_v2/list_test.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/conan/api/model.py b/conan/api/model.py index 3c606b0601f..1c3a93598ea 100644 --- a/conan/api/model.py +++ b/conan/api/model.py @@ -99,8 +99,13 @@ def from_graph(graph, graph_recipes=None, graph_binaries=None): def load_graph(graphfile, graph_recipes=None, graph_binaries=None): if not os.path.isfile(graphfile): raise ConanException(f"Graph file not found: {graphfile}") - graph = json.loads(load(graphfile)) - return MultiPackagesList._define_graph(graph, graph_recipes, graph_binaries) + try: + graph = json.loads(load(graphfile)) + return MultiPackagesList._define_graph(graph, graph_recipes, graph_binaries) + except JSONDecodeError as e: + raise ConanException(f"Graph file invalid JSON: {graphfile}\n{e}") + except Exception as e: + raise ConanException(f"Graph file broken: {graphfile}\n{e}") @staticmethod def _define_graph(graph, graph_recipes=None, graph_binaries=None): diff --git a/test/integration/command_v2/list_test.py b/test/integration/command_v2/list_test.py index f6daf9d7b6e..63b167b9289 100644 --- a/test/integration/command_v2/list_test.py +++ b/test/integration/command_v2/list_test.py @@ -43,6 +43,20 @@ def test_query_param_is_required(self): c.run("list * -p os=Linux", assert_error=True) assert "--package-query and --filter-xxx can only be done for binaries" in c.out + def test_graph_file_error(self): + # This can happen when reusing the same file in input and output + c = TestClient(light=True) + c.run("list --graph=graph.json", assert_error=True) + assert "ERROR: Graph file not found" in c.out + c.save({"graph.json": ""}) + c.run("list --graph=graph.json", assert_error=True) + assert "ERROR: Graph file invalid JSON:" in c.out + text = b'\x2b\x2f\x76\x38J\xe2nis\xa7' + with open(os.path.join(c.current_folder, "graph.json"), 'wb') as handle: + handle.write(text) + c.run("list --graph=graph.json", assert_error=True) + assert "ERROR: Graph file broken" in c.out + @pytest.fixture(scope="module") def client():