Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error messages for broken 'list --graph' arguments #16936

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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