Skip to content

Commit c03154e

Browse files
Fix ArgumentOutOfRangeException when throw is used
Co-authored-by: Andy Schwartzmeyer <[email protected]>
1 parent 2e1b598 commit c03154e

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static ErrorRecordExtensions()
3434
s_setWriteStreamProperty = Expression.Lambda<Action<PSObject>>(
3535
Expression.Call(
3636
errorObjectParameter,
37-
writeStreamProperty.GetSetMethod(),
37+
writeStreamProperty.GetSetMethod(nonPublic: true),
3838
Expression.Constant(errorStreamType)),
3939
errorObjectParameter)
4040
.Compile();

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ public static PSCommand AddDebugOutputCommand(this PSCommand psCommand)
6161

6262
public static PSCommand MergePipelineResults(this PSCommand psCommand)
6363
{
64-
// We need to do merge errors and output before rendering with an Out- cmdlet
65-
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
66-
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
67-
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
64+
if (psCommand.Commands.Count > 0)
65+
{
66+
// We need to do merge errors and output before rendering with an Out- cmdlet
67+
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
68+
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
69+
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
70+
}
6871
return psCommand;
6972
}
7073

test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ public async Task CanExecutePSCommand()
4646
[Fact]
4747
public async Task CanHandleThrow()
4848
{
49-
// TODO: Fix this so it doesn't throw!
50-
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() =>
51-
{
52-
return psesHost.ExecutePSCommandAsync(
53-
new PSCommand().AddScript("throw"),
54-
CancellationToken.None,
55-
new PowerShellExecutionOptions { ThrowOnError = false });
56-
}).ConfigureAwait(true);
49+
await psesHost.ExecutePSCommandAsync(
50+
new PSCommand().AddScript("throw"),
51+
CancellationToken.None,
52+
new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(true);
5753
}
5854

5955
[Trait("Category", "PsesInternalHost")]

0 commit comments

Comments
 (0)