Report the real exception type from Should-Throw - #2922
Merged
Conversation
Should-Throw runs the scriptblock with InvokeWithContext, so a terminating
error comes back wrapped in a MethodInvocationException. Get-ErrorObject
unwrapped it to the inner error record and reported its Exception, which for
a parameter binding failure (and a division by zero) is only a
ParentContainsErrorRecordException placeholder, not the real exception. So
Should-Throw -ExceptionType reported the wrong type, unlike Should -Throw.
Get-ErrorObject now reports the same exception that & { } would surface in
$_: the inner exception itself when the record only carries the placeholder,
and the record's own exception otherwise (Write-Error -ErrorAction Stop and
Get-Item -ErrorAction Stop wrap the original record in an
ActionPreferenceStopException, so those keep working).
Fix #2873
🤖
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.
Should-Throwruns the scriptblock withInvokeWithContext, so a terminating error comes back wrapped in aMethodInvocationException.Get-ErrorObjectunwrapped it to the inner error record and reported itsException, which for a parameter binding failure (and a division by zero) is only aParentContainsErrorRecordExceptionplaceholder, not the real exception. SoShould-Throw -ExceptionTypereported the wrong type, unlikeShould -Throw.Get-ErrorObjectnow reports the same exception that& { }would surface in$_: the inner exception itself when the record only carries the placeholder, and the record's own exception otherwise.Write-Error -ErrorAction StopandGet-Item -ErrorAction Stopwrap the original record in anActionPreferenceStopException, so those keep working.Added tests in
Should-Throw.Tests.ps1, they fail without the fix. Thanks @fflaten for the root-cause analysis in the issue.Fix #2873
🤖