Skip to content

Commit 1944c16

Browse files
sba923Amro Khalil
authored andcommitted
Adding hex format for native command exit codes (PowerShell#21067)
1 parent 9365b56 commit 1944c16

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ internal ProcessOutputObject(object data, MinishellStream stream)
133133
}
134134
}
135135

136-
#nullable enable
136+
#nullable enable
137137
/// <summary>
138138
/// This exception is used by the NativeCommandProcessor to indicate an error
139139
/// when a native command returns a non-zero exit code.
@@ -187,7 +187,7 @@ internal NativeCommandExitException(string path, int exitCode, int processId, st
187187
public int ProcessId { get; }
188188

189189
}
190-
#nullable restore
190+
#nullable restore
191191

192192
/// <summary>
193193
/// Provides way to create and execute native commands.
@@ -972,11 +972,17 @@ internal override void Complete()
972972
}
973973

974974
const string errorId = nameof(CommandBaseStrings.ProgramExitedWithNonZeroCode);
975+
#if UNIX
976+
string hexFormatStr = "0x{0:X2}";
977+
#else
978+
string hexFormatStr = "0x{0:X8}";
979+
#endif
975980

976981
string errorMsg = StringUtil.Format(
977982
CommandBaseStrings.ProgramExitedWithNonZeroCode,
978983
NativeCommandName,
979-
_nativeProcess.ExitCode);
984+
_nativeProcess.ExitCode,
985+
string.Format(CultureInfo.InvariantCulture, hexFormatStr, _nativeProcess.ExitCode));
980986

981987
var exception = new NativeCommandExitException(
982988
Path,

src/System.Management.Automation/resources/CommandBaseStrings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
however that is used for ApplicationFailedExceptions thrown when the NativeCommandProcessor fails in an unexpected way.
164164
In this case, we have a more specific error for the native command scenario, so the two are not conflated.
165165
-->
166-
<value>Program "{0}" ended with non-zero exit code: {1}.</value>
166+
<value>Program "{0}" ended with non-zero exit code: {1} ({2}).</value>
167167
</data>
168168
<data name="ShouldProcessMessage" xml:space="preserve">
169169
<value>Performing the operation "{0}" on target "{1}".</value>

test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
4848

4949
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
5050
$error[0].TargetObject | Should -BeExactly $exePath
51-
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
51+
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
5252
}
5353

5454
It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" {
@@ -61,7 +61,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
6161

6262
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
6363
$error[0].TargetObject | Should -BeExactly $exePath
64-
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
64+
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
6565
}
6666

6767
It 'Non-zero exit code generates a non-teminating error for $ErrorActionPreference = ''SilentlyContinue''' {

0 commit comments

Comments
 (0)