BUG-HUNT: [error-handling] Unhandled exceptions in robot.json_helper.load_json_file #2814

Open
opened 2026-04-04 20:37:47 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: fix/error-handling-json-helper-load-json-file
  • Commit Message: fix(error-handling): handle FileNotFoundError and JSONDecodeError in robot.json_helper.load_json_file
  • Milestone: v3.7.0
  • Parent Epic: #362

Description

The load_json_file method in robot/json_helper.py does not handle FileNotFoundError if the file path does not exist, or json.JSONDecodeError if the file contains invalid JSON. This can cause a test case to crash unexpectedly with an unhandled exception propagating to the caller.

Location

  • File: robot/json_helper.py
  • Function/Class: JsonHelper.load_json_file
  • Lines: 15–18

Evidence

@staticmethod
def load_json_file(file_path):
    """Load JSON from a file and return as a dictionary."""
    with open(file_path) as f:
        return json.load(f)

Expected Behavior

The method should catch FileNotFoundError and json.JSONDecodeError and return a sensible default value (e.g., None) to prevent the test case from crashing unexpectedly.

Actual Behavior

The method raises unhandled FileNotFoundError or json.JSONDecodeError when given a non-existent file path or a file with invalid JSON content.

Suggested Fix

Wrap the file operations in a try...except block:

import json
from typing import Optional

@staticmethod
def load_json_file(file_path: str) -> Optional[dict]:
    """Load JSON from a file and return as a dictionary."""
    try:
        with open(file_path) as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return None

Severity Assessment

  • Impact: A test case will crash if it provides a non-existent file path or a file with invalid JSON.
  • Likelihood: Medium — test data files might be missing or malformed.
  • Priority: Medium

Subtasks

  • Add try...except block to JsonHelper.load_json_file catching FileNotFoundError and json.JSONDecodeError
  • Add explicit type annotations to load_json_file signature (return type Optional[dict], parameter type str)
  • Write Behave unit tests covering: file not found, invalid JSON, and valid JSON scenarios
  • Write Robot Framework integration test verifying the fix end-to-end
  • Verify nox -e typecheck passes with no type errors
  • Verify nox -e lint passes with no lint errors

Definition of Done

  • JsonHelper.load_json_file catches FileNotFoundError and returns None instead of raising
  • JsonHelper.load_json_file catches json.JSONDecodeError and returns None instead of raising
  • All type annotations are explicit and complete (no implicit Any)
  • Behave unit tests added for all three scenarios (missing file, invalid JSON, valid JSON)
  • Robot Framework integration test updated or added to cover the fix
  • All nox stages pass
  • Coverage >= 97%

Automated by CleverAgents Bot
Supervisor: Unknown | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/error-handling-json-helper-load-json-file` - **Commit Message**: `fix(error-handling): handle FileNotFoundError and JSONDecodeError in robot.json_helper.load_json_file` - **Milestone**: v3.7.0 - **Parent Epic**: #362 ## Description The `load_json_file` method in `robot/json_helper.py` does not handle `FileNotFoundError` if the file path does not exist, or `json.JSONDecodeError` if the file contains invalid JSON. This can cause a test case to crash unexpectedly with an unhandled exception propagating to the caller. ### Location - **File**: `robot/json_helper.py` - **Function/Class**: `JsonHelper.load_json_file` - **Lines**: 15–18 ### Evidence ```python @staticmethod def load_json_file(file_path): """Load JSON from a file and return as a dictionary.""" with open(file_path) as f: return json.load(f) ``` ### Expected Behavior The method should catch `FileNotFoundError` and `json.JSONDecodeError` and return a sensible default value (e.g., `None`) to prevent the test case from crashing unexpectedly. ### Actual Behavior The method raises unhandled `FileNotFoundError` or `json.JSONDecodeError` when given a non-existent file path or a file with invalid JSON content. ### Suggested Fix Wrap the file operations in a `try...except` block: ```python import json from typing import Optional @staticmethod def load_json_file(file_path: str) -> Optional[dict]: """Load JSON from a file and return as a dictionary.""" try: with open(file_path) as f: return json.load(f) except (FileNotFoundError, json.JSONDecodeError): return None ``` ### Severity Assessment - **Impact**: A test case will crash if it provides a non-existent file path or a file with invalid JSON. - **Likelihood**: Medium — test data files might be missing or malformed. - **Priority**: Medium ## Subtasks - [ ] Add `try...except` block to `JsonHelper.load_json_file` catching `FileNotFoundError` and `json.JSONDecodeError` - [ ] Add explicit type annotations to `load_json_file` signature (return type `Optional[dict]`, parameter type `str`) - [ ] Write Behave unit tests covering: file not found, invalid JSON, and valid JSON scenarios - [ ] Write Robot Framework integration test verifying the fix end-to-end - [ ] Verify `nox -e typecheck` passes with no type errors - [ ] Verify `nox -e lint` passes with no lint errors ## Definition of Done - [ ] `JsonHelper.load_json_file` catches `FileNotFoundError` and returns `None` instead of raising - [ ] `JsonHelper.load_json_file` catches `json.JSONDecodeError` and returns `None` instead of raising - [ ] All type annotations are explicit and complete (no implicit `Any`) - [ ] Behave unit tests added for all three scenarios (missing file, invalid JSON, valid JSON) - [ ] Robot Framework integration test updated or added to cover the fix - [ ] All nox stages pass - [ ] Coverage >= 97% --- **Automated by CleverAgents Bot** Supervisor: Unknown | Agent: ca-new-issue-creator
freemo added this to the v3.7.0 milestone 2026-04-04 20:37:53 +00:00
freemo removed this from the v3.7.0 milestone 2026-04-07 00:42:25 +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.

Blocks
#362 Epic: Security & Safety Hardening
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#2814
No description provided.