From 04fbd7c557af0d51012098c63b4ecbda17225d4d Mon Sep 17 00:00:00 2001 From: aryan Date: Fri, 20 Mar 2026 14:05:32 -0400 Subject: [PATCH] fix: improve CLI error messages in register and policy commands The register command did a bare import of AgentIdentity inside the function body with no error handling. If agentmesh is not installed, Python dumps a raw traceback instead of a clear message. Wrapped the import in a try/except ImportError block that tells the user what to run. The policy command caught all exceptions as a single bare Exception and printed only the error object. Split it into FileNotFoundError, JSON/YAML parse errors, and a general fallback so each case gives a message that points to the actual problem. Related to issue #307. --- packages/agent-mesh/src/agentmesh/cli/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/agent-mesh/src/agentmesh/cli/main.py b/packages/agent-mesh/src/agentmesh/cli/main.py index 83eca71a..9f15795e 100644 --- a/packages/agent-mesh/src/agentmesh/cli/main.py +++ b/packages/agent-mesh/src/agentmesh/cli/main.py @@ -258,7 +258,11 @@ def register(agent_dir: str, name: str = None): console.print(f"\n[bold blue]📝 Registering agent: {agent_name}[/bold blue]\n") # Simulate registration - from agentmesh.identity import AgentIdentity + try: + from agentmesh.identity import AgentIdentity + except ImportError: + console.print("[red]Error: agentmesh is not installed. Run: pip install agentmesh[/red]") + return identity = AgentIdentity.create(agent_name) console.print(f" [green]✓[/green] Generated identity: {identity.did}") @@ -356,8 +360,12 @@ def policy(policy_file: str, validate: bool): console.print(f"\n[green]Successfully loaded {len(policies)} policies[/green]") + except FileNotFoundError: + console.print(f"[red]Error: Policy file not found: {policy_file}[/red]") + except (json.JSONDecodeError, yaml.YAMLError) as e: + console.print(f"[red]Error: Failed to parse {policy_file} — {e}[/red]") except Exception as e: - console.print(f"[red]Error: {e}[/red]") + console.print(f"[red]Error loading policy: {e}[/red]") @app.command()