Fix error handling for non-subject value and value stratifiers#993
Merged
JPercival merged 6 commits intoApr 24, 2026
Merged
Conversation
|
Formatting check succeeded! |
Wrap CqlException from unresolvable stratifier expressions in a new StratifierExpressionNotFoundException, and validate non-function expression result types against ALLOWED_STRATIFIER_BOOLEAN_BASIS_TYPES by removing the blanket NON_SUBJECT_VALUE bypass in R4PopulationBasisValidator. Both new exceptions are domain RuntimeExceptions (not HAPI FHIR REST exceptions) per architecture decision. Add 7 tests covering scalar-only, function-only, non-existent expression, wrong-type expression, wrong function param type, and boolean-basis list expression scenarios. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ct-value-stratifier-error-handling-bug
Deduplicate result types in error messages (e.g. [Encounter, Encounter] becomes [Encounter]) and add expected-type guidance: "Expected a scalar type" for VALUE stratifiers, "Expected a scalar or scalar-returning function" for NON_SUBJECT_VALUE. Replace InvalidRequestException with InvalidStratifierExpressionTypeException in PopulationBasisValidator where validation logic now lives after upstream refactor. Update assertions in R4PopulationBasisValidatorTest and MeasureStratifierTest to match new message format, and ensure both branches of buildValueStratifierErrorMessage are covered in MeasureStratifierTest. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
JPercival
approved these changes
Apr 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
PR #913 (issue #909) added support for non-subject value stratifiers to use both CQL functions and scalar expressions, but left gaps in error handling: expressions that don't exist in the CQL library produced raw
CqlExceptionstack traces, and expressions returning invalid types (e.g. a list of FHIR resources instead of a scalar) were silently accepted, producing nonsense strata. This MR closes those gaps with proper validation and domain-specific exceptions.StratifierExpressionNotFoundException: InFunctionEvaluationHandler.processNonSubValueStratifier(), the call toisExpressionFunctionRef()now catchesCqlExceptionand re-throws as a contextualStratifierExpressionNotFoundExceptionthat includes the expression name and measure URL.PopulationBasisValidator.validateExpressionResultType()now throwsInvalidStratifierExpressionTypeException(instead ofInvalidRequestException) for value stratifier expressions whose result types are not in the allowed set (e.g. Encounter instead of String/Code/Integer). This applies to both VALUE and NON_SUBJECT_VALUE stratifier types.StratifierExpressionNotFoundExceptionandInvalidStratifierExpressionTypeExceptionextendRuntimeExceptiondirectly, not HAPI FHIR REST exceptions, per the project's architecture decision.Code Review Suggestions
StratifierExpressionNotFoundExceptionandInvalidStratifierExpressionTypeExceptionare caught by the catch-allExceptionhandlers inMeasureEvaluationResultHandlerand routed to contained OperationOutcome (not thrown to the caller). The tests assert OperationOutcome, but confirm the catch-all behavior is intentional for these specific exception types vs. failing fast.prettyDistinctClassNames()helper deduplicates byClass::getSimpleName. Confirm there are no cases where two distinct classes share a simple name (e.g.org.hl7.fhir.r4.model.Encountervs a hypothetical CQLEncountertype) that would lose information in the error message.InvalidRequestException | InvalidStratifierExpressionTypeExceptioninR4PopulationBasisValidatorTest.validateStratifierBasisTypeErrorPath()is a test-only convenience. Confirm the production code paths throw the correct specific exception for each stratifier type:InvalidRequestExceptionfor criteria-based,InvalidStratifierExpressionTypeExceptionfor value/non-subject-value.InvalidStratifierExpressionTypeExceptionhas two constructors: aString messageform and a structured form with 5 parameters. Only theString messageform is used in production code. Confirm whether the structured constructor should be kept or removed.QA Test Suggestions
Setup
Evaluate a FHIR R4 Measure with
$evaluate-measureusing the test data from theMeasureTest/inputdirectory (LibrarySimple CQL library, patient-9 with 2 encounters). All tests use cohort scoring.definethat returns[Encounter] E(a list of resources, not a scalar). Verify the MeasureReport contains a contained OperationOutcome with a message indicating "Expected a scalar or scalar-returning function".definereturning a String (e.g. "Patient Age Bracket" returning "21--41"). Verify the MeasureReport completes successfully with the expected stratum values.define functionthat takes an Encounter and returns a String (e.g. "Encounter Status Stratifier"). Verify the MeasureReport completes with per-encounter strata (e.g. "finished", "in-progress").Interval<DateTime>instead ofEncounter). Verify the MeasureReport contains a contained OperationOutcome referencing "CQL Exception while evaluating function".MeasureStratifierTestsuite and verify no regressions in existing cohort, proportion, and ratio stratifier scenarios (criteria-based, value-based, multi-component).