Skip to content

fix: add key validation and preserve exception chains in workflow models#226

Open
crazywriter1 wants to merge 1 commit intoOpenGradient:mainfrom
crazywriter1:fix/workflow-models-error-handling
Open

fix: add key validation and preserve exception chains in workflow models#226
crazywriter1 wants to merge 1 commit intoOpenGradient:mainfrom
crazywriter1:fix/workflow-models-error-handling

Conversation

@crazywriter1
Copy link
Copy Markdown

Summary

  • All 7 workflow model functions (e.g. read_btc_1_hour_price_forecast) accessed result.numbers["key"] directly without checking if the key exists, causing a bare KeyError with no context when the workflow output is missing the expected field
  • Added _extract_number() helper that validates key existence before access and raises a descriptive KeyError listing available keys
  • read_workflow_wrapper() had a broad except Exception that converted every error (including KeyError) into a generic RuntimeError, losing the original error type and traceback
  • Now catches KeyError separately so callers can distinguish "missing output key" from "network/contract failure"
  • Added from e to both exception paths to preserve the full exception chain for debugging

Changed files

  • src/opengradient/workflow_models/workflow_models.py — added _extract_number() helper, replaced all 7 raw x.numbers["key"] accesses
  • src/opengradient/workflow_models/utils.py — added KeyError handler, added from e chaining, updated docstring
  • tests/workflow_models_test.py — new test file with 7 tests

Test plan

  • test_extracts_valid_key — happy path extraction
  • test_raises_on_missing_key — KeyError with descriptive message
  • test_raises_on_missing_numbers_attr — handles result without .numbers
  • test_reports_available_keys — error message lists available keys
  • test_keyerror_preserved_not_converted_to_runtime — KeyError stays KeyError
  • test_runtime_error_chains_original__cause__ is set via from e
  • test_success_returns_workflow_output — happy path returns correct output
  • All existing tests pass

_extract_number() helper validates key existence before access, preventing
bare KeyError. read_workflow_wrapper() now catches KeyError separately and
uses 'from e' to preserve the original exception chain.

Signed-off-by: crazywriter1 <crazywriter1@users.noreply.github.com>
@crazywriter1
Copy link
Copy Markdown
Author

@adambalogh

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant