fix(error-handling): handle json.JSONDecodeError in robot.json_helper.parse_json #2811

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

Metadata

  • Branch: fix/v3.8.0-json-helper-decode-error
  • Commit Message: fix(error-handling): handle json.JSONDecodeError in robot.json_helper.parse_json
  • Milestone: v3.8.0
  • Parent Epic: #362

Background and context

The parse_json function in robot/json_helper.py calls json.loads without any error handling. If a test case passes a malformed or invalid JSON string to this function, a json.JSONDecodeError will propagate uncaught, crashing the test case unexpectedly. This was discovered during a bug-hunt audit of the robot/ helper layer.

Per CONTRIBUTING.md's error-handling guidelines, exceptions should only be caught when they can be meaningfully handled. In this case, the function is a low-level parsing utility used by Robot Framework test cases — an unhandled decode error here provides no useful diagnostic context to the test runner and causes an abrupt, confusing failure.

Current behavior (for bugs)

When parse_json receives an invalid JSON string (e.g., "not json"), json.loads raises json.JSONDecodeError, which propagates uncaught out of the function and crashes the calling test case.

# robot/json_helper.py — lines 6–8
def parse_json(json_string):
    """Parse a JSON string and return as a dictionary."""
    return json.loads(json_string)

Reproduction steps:

  1. Call parse_json("invalid json string") from any Robot Framework test.
  2. Observe json.JSONDecodeError raised and test case aborted.

Expected behavior

The function should catch json.JSONDecodeError and either:

  • Return None to signal a parse failure without crashing, or
  • Re-raise as a more descriptive, domain-specific exception with context about the bad input.

The chosen approach must be consistent with the project's fail-fast principles while still providing a meaningful signal to callers.

Acceptance criteria

  • parse_json no longer raises an unhandled json.JSONDecodeError when given invalid input
  • The chosen error-handling strategy (return None or raise domain exception) is documented in the function's docstring
  • A unit test covers the invalid-JSON code path and asserts the expected return value or exception type
  • A Robot Framework test scenario covers the invalid-JSON code path
  • nox (all default sessions) passes with no errors
  • Coverage >= 97%

Supporting information

  • File: robot/json_helper.py
  • Function: parse_json
  • Lines: 6–8
  • Related Epic: #362 — Security & Safety Hardening (covers explicit exception handling)
  • Severity: Medium — test cases may crash on malformed test data

Subtasks

  • Investigate whether callers of parse_json expect None or a raised exception on bad input
  • Implement try/except json.JSONDecodeError block in parse_json with the agreed-upon strategy
  • Update the function's docstring to document the error-handling behaviour
  • Tests (Unit): Add test for parse_json with invalid JSON input asserting correct behaviour
  • Tests (Robot): Add Robot Framework scenario for invalid JSON input to parse_json
  • Verify coverage >= 97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass.
  • Coverage >= 97%.

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

## Metadata - **Branch**: `fix/v3.8.0-json-helper-decode-error` - **Commit Message**: `fix(error-handling): handle json.JSONDecodeError in robot.json_helper.parse_json` - **Milestone**: v3.8.0 - **Parent Epic**: #362 ## Background and context The `parse_json` function in `robot/json_helper.py` calls `json.loads` without any error handling. If a test case passes a malformed or invalid JSON string to this function, a `json.JSONDecodeError` will propagate uncaught, crashing the test case unexpectedly. This was discovered during a bug-hunt audit of the `robot/` helper layer. Per CONTRIBUTING.md's error-handling guidelines, exceptions should only be caught when they can be meaningfully handled. In this case, the function is a low-level parsing utility used by Robot Framework test cases — an unhandled decode error here provides no useful diagnostic context to the test runner and causes an abrupt, confusing failure. ## Current behavior (for bugs) When `parse_json` receives an invalid JSON string (e.g., `"not json"`), `json.loads` raises `json.JSONDecodeError`, which propagates uncaught out of the function and crashes the calling test case. ```python # robot/json_helper.py — lines 6–8 def parse_json(json_string): """Parse a JSON string and return as a dictionary.""" return json.loads(json_string) ``` **Reproduction steps:** 1. Call `parse_json("invalid json string")` from any Robot Framework test. 2. Observe `json.JSONDecodeError` raised and test case aborted. ## Expected behavior The function should catch `json.JSONDecodeError` and either: - Return `None` to signal a parse failure without crashing, **or** - Re-raise as a more descriptive, domain-specific exception with context about the bad input. The chosen approach must be consistent with the project's fail-fast principles while still providing a meaningful signal to callers. ## Acceptance criteria - [ ] `parse_json` no longer raises an unhandled `json.JSONDecodeError` when given invalid input - [ ] The chosen error-handling strategy (return `None` or raise domain exception) is documented in the function's docstring - [ ] A unit test covers the invalid-JSON code path and asserts the expected return value or exception type - [ ] A Robot Framework test scenario covers the invalid-JSON code path - [ ] `nox` (all default sessions) passes with no errors - [ ] Coverage >= 97% ## Supporting information - **File**: `robot/json_helper.py` - **Function**: `parse_json` - **Lines**: 6–8 - **Related Epic**: #362 — Security & Safety Hardening (covers explicit exception handling) - **Severity**: Medium — test cases may crash on malformed test data ## Subtasks - [ ] Investigate whether callers of `parse_json` expect `None` or a raised exception on bad input - [ ] Implement `try/except json.JSONDecodeError` block in `parse_json` with the agreed-upon strategy - [ ] Update the function's docstring to document the error-handling behaviour - [ ] Tests (Unit): Add test for `parse_json` with invalid JSON input asserting correct behaviour - [ ] Tests (Robot): Add Robot Framework scenario for invalid JSON input to `parse_json` - [ ] Verify coverage >= 97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass. - Coverage >= 97%. --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-new-issue-creator
freemo added this to the v3.8.0 milestone 2026-04-04 20:35:09 +00:00
freemo removed this from the v3.8.0 milestone 2026-04-07 00:42:32 +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#2811
No description provided.