Change formatter for ErrorRecord

Fix #1465

On Linux, it's common to use error stream (2) as an information stream.
There is no errors in this case, it's just a way to report info.
When PowerShell redirects error stream, it wraps all output from stream 2
in NativeCommandError ErrorRecord.

That doesn't happen for interactive experience (when there is no
redirection), but it shows up in logs, scripting and non-console
powershell hosts (i.e. PowerShell ISE).

Here we change the formatter, so we are keeping the ErrorRecords
objects, but we are changing their default representation.

If it's a NativeCommandError, we don't use the regular ErrorRecord
formatter. It allows us keep ErrorRecord object,
but in redirection case it will look exectly like on the screen.

Also, we are change NativeCommandErrorMessage to use one line per
ErrorRecord to enable this scenario.
This commit is contained in:
Sergei Vorobev
2016-08-01 18:39:57 -07:00
parent 9742b64170
commit a12eff96c0
2 changed files with 4 additions and 10 deletions
@@ -720,7 +720,7 @@ namespace System.Management.Automation.Runspaces
CustomControl.Create(outOfBand: true)
.StartEntry()
.AddScriptBlockExpressionBinding(@"
if ($_.FullyQualifiedErrorId -ne ""NativeCommandErrorMessage"" -and $ErrorView -ne ""CategoryView"")
if (($_.FullyQualifiedErrorId -ne ""NativeCommandErrorMessage"" -and $_.FullyQualifiedErrorId -ne ""NativeCommandError"") -and $ErrorView -ne ""CategoryView"")
{
$myinv = $_.InvocationInfo
if ($myinv -and $myinv.MyCommand)
@@ -767,7 +767,7 @@ namespace System.Management.Automation.Runspaces
}
")
.AddScriptBlockExpressionBinding(@"
if ($_.FullyQualifiedErrorId -eq ""NativeCommandErrorMessage"") {
if ($_.FullyQualifiedErrorId -eq ""NativeCommandErrorMessage"" -or $_.FullyQualifiedErrorId -eq ""NativeCommandError"") {
$_.Exception.Message
}
else
@@ -1880,17 +1880,11 @@ namespace System.Management.Automation
//
// Wrap the rest of the output in ErrorRecords with the "NativeCommandErrorMessage" error ID
//
char[] buffer = new char[4096];
int read = 0;
while ((read = _streamReader.Read(buffer, 0, buffer.Length)) != 0)
while ((line = _streamReader.ReadLine()) != null)
{
StringBuilder errorMessage = new StringBuilder().Append(buffer, 0, read);
AddObjectToWriter(
new ErrorRecord(
new RemoteException(errorMessage.ToString()),
new RemoteException(line),
"NativeCommandErrorMessage",
ErrorCategory.NotSpecified,
null),