Skip to content

[Bug] Invalid --output-format causes unhandled ValueError traceback instead of user-friendly error #32

@Prodesire

Description

@Prodesire

Bug Description

When using --output-format with an invalid value (including case-sensitive mismatches like TEXT instead of text), the CLI crashes with a raw Python traceback instead of displaying a user-friendly error message.

Steps to Reproduce

  1. Run iac-code -p "hello" --output-format invalid
  2. Or run iac-code -p "hello" --output-format TEXT

Expected Behavior

A clean error message like:

Error: Invalid output format 'invalid'. Valid formats: text, json, stream-json

Actual Behavior

A full Python traceback is displayed:

Traceback (most recent call last):
  File ".../bin/iac-code", line 6, in <module>
    sys.exit(app())
  ...
  File ".../iac_code/cli/main.py", line 191, in main
    fmt = OutputFormat(output_format)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../enum.py", line 1171, in __new__
    raise ve_exc
ValueError: 'invalid' is not a valid OutputFormat

Root Cause

In src/iac_code/cli/main.py line 191, OutputFormat(output_format) is called without a try/except:

fmt = OutputFormat(output_format)

A fix would be to catch the ValueError and print a friendly message:

try:
    fmt = OutputFormat(output_format)
except ValueError:
    valid = ", ".join(f.value for f in OutputFormat)
    typer.echo(f"Invalid output format '{output_format}'. Valid formats: {valid}", err=True)
    raise typer.Exit(1)

Operating System

macOS

Python Version

3.12.7

iac-code Version

0.2.3

LLM Provider

Alibaba Cloud (DashScope / Qwen)

IaC Type

Not applicable

Additional Context

This also affects case-sensitive inputs — a user typing --output-format JSON gets a crash instead of a helpful hint. Consider normalizing the input with .lower() before validation.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions