Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1651,28 +1651,35 @@ internal static void CheckActionPreference(FunctionContext funcContext, Exceptio
}
else if (ExceptionCannotBeStoppedContinuedOrIgnored(rte, context))
{
// If we throw this error but don't mark it for prompt suppression
// it might be recaught later when the error action preference has changed.
// Instead we need to mark it here to preserve the EAP semantics from
// where it was originally thrown
if (preference == ActionPreference.Stop)
{
rte.SuppressPromptInInterpreter = true;
}

throw rte;
}
else if (preference == ActionPreference.Inquire && !rte.SuppressPromptInInterpreter)
{
preference = InquireForActionPreference(rte.Message, context);
}

if ((preference == ActionPreference.SilentlyContinue) ||
(preference == ActionPreference.Ignore))
switch (preference)
{
return;
}

if (preference == ActionPreference.Stop)
{
// The interpreter prompt CommandBaseStrings:InquireHalt
// should be suppressed when this flag is set. This will be set
// when this prompt has already occurred and Break was chosen,
// or for ActionPreferenceStopException in all cases.
rte.SuppressPromptInInterpreter = true;
case ActionPreference.SilentlyContinue:
case ActionPreference.Ignore:
return;

throw rte;
case ActionPreference.Stop:
// The interpreter prompt CommandBaseStrings:InquireHalt
// should be suppressed when this flag is set. This will be set
// when this prompt has already occurred and Break was chosen,
// or for ActionPreferenceStopException in all cases.
rte.SuppressPromptInInterpreter = true;
throw rte;
}

if (!anyTrapHandlers && rte.WasThrownFromThrowStatement)
Expand Down
142 changes: 142 additions & 0 deletions test/powershell/Language/Scripting/ActionPreference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,148 @@ Describe "Tests for (error, warning, etc) action preference" -Tags "CI" {
$err.FullyQualifiedErrorId | Should -BeExactly "ParameterBindingFailed,Microsoft.PowerShell.Commands.TestPathCommand"
$err.Exception.InnerException.InnerException | Should -BeOfType "System.Management.Automation.PSInvalidCastException"
}

It 'Correctly interprets ErrorActionPreference = ''<EAP>'' on Write-Error when it''s reset in a finally block' -TestCases @(
@{ EAP = 'Stop' }
@{ EAP = 'Continue' }
@{ EAP = 'SilentlyContinue' }
@{ EAP = 'Ignore' }
) {
param([System.Management.Automation.ActionPreference]$EAP)

$error.Clear()
$completedCommand = $false
$completedFinally = $false
$threw = $false

try
{
$oldEAP = $ErrorActionPreference
$ErrorActionPreference = $EAP

try
{
Write-Error 'error'
$completedCommand = $true
}
finally
{
$ErrorActionPreference = $oldEAP
$completedFinally = $true
}
}
catch
{
$err = $_
$threw = $true
}

switch ($EAP)
{
'Stop'
{
$err.FullyQualifiedErrorId | Should -BeExactly 'Microsoft.PowerShell.Commands.WriteErrorException'
$err.Exception.Message | Should -BeExactly 'error'
$completedCommand | Should -BeFalse
$completedFinally | Should -BeTrue
$threw | Should -BeTrue
break
}

'Ignore'
{
$completedCommand | Should -BeTrue
$completedFinally | Should -BeTrue
$threw | Should -BeFalse
$err | Should -BeNullOrEmpty
$error.Count | Should -Be 0
break
}

{ $_ -in 'Continue','SilentlyContinue' }
{
$lastError = $error[0]
$completedCommand | Should -BeTrue
$completedFinally | Should -BeTrue
$threw | Should -BeFalse
$lastError.FullyQualifiedErrorId | Should -BeExactly 'Microsoft.PowerShell.Commands.WriteErrorException'
$lastError.Exception.Message | Should -BeExactly 'error'
}
}

}

It 'Correctly interprets ErrorActionPreference = ''<EAP>'' on a parameter binding error when it''s reset in a finally block' -TestCases @(
@{ EAP = 'Stop' }
@{ EAP = 'Continue' }
@{ EAP = 'SilentlyContinue' }
@{ EAP = 'Ignore' }
) {
param([System.Management.Automation.ActionPreference]$EAP)

$error.Clear()
$completedCommand = $false
$completedFinally = $false
$threw = $false

try
{
$oldEAP = $ErrorActionPreference
$ErrorActionPreference = $EAP

try
{
Get-Process -Banana
$completedCommand = $true
}
finally
{
$ErrorActionPreference = $oldEAP
$completedFinally = $true
}
}
catch
{
$err = $_
$threw = $true
}

switch ($EAP)
{
'Stop'
{
$err.FullyQualifiedErrorId | Should -BeExactly 'NamedParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand'
$err.CategoryInfo.Activity | Should -BeExactly 'Get-Process'
$err.CategoryInfo.Category | Should -BeExactly 'InvalidArgument'
$err.CategoryInfo.Reason | Should -BeExactly 'ParameterBindingException'
$completedCommand | Should -BeFalse
$completedFinally | Should -BeTrue
$threw | Should -BeTrue
break
}

'Ignore'
{
$completedCommand | Should -BeTrue
$completedFinally | Should -BeTrue
$threw | Should -BeFalse
$err | Should -BeNullOrEmpty
$error.Count | Should -Be 0
}

{ $_ -in 'Continue','SilentlyContinue' }
{
$completedCommand | Should -BeTrue
$completedFinally | Should -BeTrue
$threw | Should -BeFalse
$lastError = $error[0]
$lastError.FullyQualifiedErrorId | Should -BeExactly 'NamedParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand'
$lastError.CategoryInfo.Activity | Should -BeExactly 'Get-Process'
$lastError.CategoryInfo.Category | Should -BeExactly 'InvalidArgument'
$lastError.CategoryInfo.Reason | Should -BeExactly 'ParameterBindingException'
}
}
}
}

Describe 'ActionPreference.Break tests' -Tag 'CI' {
Expand Down