Skip to content

Commit

Permalink
improve error messages for broken 'list --graph' arguments (#16936)
Browse files Browse the repository at this point in the history
Changelog: Fix: Better error messages when ``conan list
--graph=<graph-json-file>`` file has issues
Docs: Omit

Close #16921
  • Loading branch information
ErniGH authored Sep 5, 2024
2 parents 7a273d6 + 97117d6 commit 6acce7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions conan/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions test/integration/command_v2/list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 6acce7b

Please sign in to comment.