fix: clear error when rule Condition is null - #89
Merged
Conversation
Test-FeatureFlag/Test-Condition threw an opaque 'You cannot call a method on a null-valued expression' error when a rule's Condition was $null (omitted or misspelled key). ConditionTransformAttribute called .GetType() on the null input before any of its type checks ran. ConditionTransformAttribute.Transform() now null-guards up front and throws a clear ArgumentNullException naming the problem: the rule must define a Condition. FeatureFlagTransformAttribute gained the same null guard for consistency (same GetType()-on-null pattern for a null -FeatureFlag). Added coverage: Test-Condition -Condition $null, and a Test-FeatureFlag rule with no Condition, both assert the new clear error message.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Gatekeeper’s argument transformation error handling so malformed feature flag rules (specifically missing/null Condition) fail with a clear, actionable ArgumentNullException message instead of an opaque null-method invocation error.
Changes:
- Added upfront
$nullguards inConditionTransformAttribute.Transform()andFeatureFlagTransformAttribute.Transform()to avoid.GetType()-on-null crashes and emit clearer errors. - Added Pester coverage for
Test-Condition -Condition $nulland forTest-FeatureFlagevaluating a rule with a missingCondition. - Documented the fix in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Gatekeeper/Classes/FeatureFlag.ps1 | Adds null guards in argument transformation attributes to produce clear exceptions for null Condition / FeatureFlag. |
| tests/Test-FeatureFlag.tests.ps1 | Adds a regression test ensuring a rule missing Condition throws a clear error during Test-FeatureFlag. |
| tests/Classes.tests.ps1 | Adds a regression test ensuring Test-Condition -Condition $null throws a clear error. |
| CHANGELOG.md | Notes the behavior change and references issue #83. |
Comment on lines
+265
to
+268
| if ($null -eq $inputData) { | ||
| throw [System.ArgumentNullException]::new('Condition', | ||
| "Rule has no Condition. Each rule must define a 'Condition'.") | ||
| } |
Comment on lines
+239
to
+242
| if ($null -eq $inputData) { | ||
| throw [System.ArgumentNullException]::new('FeatureFlag', | ||
| "FeatureFlag cannot be null.") | ||
| } |
Test Results 4 files 448 suites 12s ⏱️ Results for commit 13a5b4f. |
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
Fixes #83.
Test-FeatureFlag/Test-Conditionthrew an opaqueYou cannot call a method on a null-valued expressionerror when a rule'sConditionwas$null(e.g. theConditionkey omitted or misspelled).ConditionTransformAttribute.Transform()called.GetType()on the raw input before any of its-istype checks, so a null input never got a chance to hit a meaningful branch.Fix
Classes/FeatureFlag.ps1:ConditionTransformAttribute.Transform()now null-guards up front and throws a clearArgumentNullException— "Rule has no Condition. Each rule must define a 'Condition'." — instead of the opaque binder error.FeatureFlagTransformAttribute.Transform()got the same guard for consistency (identicalGetType()-on-null crash risk for a null-FeatureFlag).Test-Condition -Condition $nullasserts the new clear message.Test-FeatureFlagwith a rule that has noConditionasserts the same.CHANGELOG.mdupdated.Verification
Reproduced the exact repro from #83:
Full build/test (
./build.ps1 -Task Test): 368 passed, 0 failed, 5 skipped (pre-existing), Analyze clean (pre-existing informational/warning rules only).