Skip to content

Handle invalid structure YAML in generate without tracebacks or partial side effects #164

Closed

Description

@httpdss

Summary

structkit generate can raise raw tracebacks when the structure definition is invalid YAML or when the YAML top-level value is not a mapping. It also creates the output directory before fully validating the structure config, which can leave partial side effects after a config-load failure.

Evidence

Observed locally with the current code:

python -m structkit.main generate --non-interactive invalid.yaml out
Generating structure
  Structure definition: /tmp/.../invalid.yaml
  Base path: /tmp/.../out
Creating base path: /tmp/.../out
Traceback ...
yaml.parser.ParserError: while parsing a flow node

For a YAML file whose top-level value is a list:

python -m structkit.main generate --non-interactive list.yaml out
Traceback ...
AttributeError: 'list' object has no attribute 'get'

Relevant code:

  • structkit/commands/generate.py:_load_yaml_config does not catch yaml.YAMLError/OSError.
  • GenerateCommand.execute assumes the loaded config has .get(...).
  • execute creates base_path before loading/validating the config.

Proposed fix

  • Make _load_yaml_config return a structured clean failure or raise a domain error for invalid YAML/read errors.
  • Validate that loaded config is a mapping before accessing .get.
  • Load and validate the structure config before creating base_path where practical.
  • Convert expected config-load failures into ❗ ... messages and exit code 1.

Acceptance criteria

  • Invalid YAML in a structure exits 1 with no Traceback.
  • Non-mapping top-level YAML exits 1 with a clear message such as top-level YAML content must be a mapping.
  • Missing local structure files have clean errors.
  • Generation does not create the output directory when config loading/shape validation fails.
  • Tests cover invalid YAML, top-level list YAML, and missing file cases.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions