BUG-HUNT: [error-handling] Generic exception handling in YAML loading #1321

Open
opened 2026-04-02 16:49:20 +00:00 by freemo · 0 comments
Owner

Bug Report: [error-handling] — Generic exception handling in YAML loading

Severity Assessment

  • Impact: Catching a generic Exception can hide unexpected errors and make debugging difficult.
  • Likelihood: Low. The code is trying to load a YAML file, so a YAMLError is the most likely exception.
  • Priority: Low

Location

  • File: src/cleveragents/actor/config.py
  • Function/Class: _load_v2_yaml_content
  • Lines: 100-102

Description

The _load_v2_yaml_content function in config.py catches a generic Exception. This is too broad and can hide unexpected errors. It should catch a more specific exception, such as yaml.YAMLError.

Evidence

    @staticmethod
    def _load_v2_yaml_content(text: str) -> dict[str, Any]:
        if yaml is None:
            raise ValueError("PyYAML is required for YAML actor configs")

        raw: Any
        try:
            if "{%" in text or "{{" in text:
                # ... (templating logic)
            else:
                raw = yaml.safe_load(text)
        except Exception as exc:  # pragma: no cover - defensive
            raise ValueError(f"Failed to parse config: {exc}") from exc

        if raw is None:
            raw = {}
        if not isinstance(raw, dict):
            raise ValueError("Config must be a JSON or YAML object")

        interpolated = ActorConfiguration._interpolate_env_vars(raw)
        if not isinstance(interpolated, dict):
            raise ValueError("Config must be a JSON or YAML object")
        return cast(dict[str, Any], interpolated)

Expected Behavior

The function should catch a more specific exception, such as yaml.YAMLError.

Actual Behavior

The function catches a generic Exception.

Suggested Fix

Change except Exception as exc: to except yaml.YAMLError as exc:.

Category

error-handling

## Bug Report: [error-handling] — Generic exception handling in YAML loading ### Severity Assessment - **Impact**: Catching a generic `Exception` can hide unexpected errors and make debugging difficult. - **Likelihood**: Low. The code is trying to load a YAML file, so a `YAMLError` is the most likely exception. - **Priority**: Low ### Location - **File**: `src/cleveragents/actor/config.py` - **Function/Class**: `_load_v2_yaml_content` - **Lines**: 100-102 ### Description The `_load_v2_yaml_content` function in `config.py` catches a generic `Exception`. This is too broad and can hide unexpected errors. It should catch a more specific exception, such as `yaml.YAMLError`. ### Evidence ```python @staticmethod def _load_v2_yaml_content(text: str) -> dict[str, Any]: if yaml is None: raise ValueError("PyYAML is required for YAML actor configs") raw: Any try: if "{%" in text or "{{" in text: # ... (templating logic) else: raw = yaml.safe_load(text) except Exception as exc: # pragma: no cover - defensive raise ValueError(f"Failed to parse config: {exc}") from exc if raw is None: raw = {} if not isinstance(raw, dict): raise ValueError("Config must be a JSON or YAML object") interpolated = ActorConfiguration._interpolate_env_vars(raw) if not isinstance(interpolated, dict): raise ValueError("Config must be a JSON or YAML object") return cast(dict[str, Any], interpolated) ``` ### Expected Behavior The function should catch a more specific exception, such as `yaml.YAMLError`. ### Actual Behavior The function catches a generic `Exception`. ### Suggested Fix Change `except Exception as exc:` to `except yaml.YAMLError as exc:`. ### Category error-handling
freemo self-assigned this 2026-04-02 18:45:23 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#1321
No description provided.