Skip to content

Commit d8d908d

Browse files
committed
Write RuntimeExceptions to PSHostUserInterface
This change causes RuntimeExceptions thrown during PowerShellContext.ExecuteCommand calls to be written to the host. Previously these error messages were not being written so users did not see error output for any commands using the "-ErrorAction Stop" parameter.
1 parent 3a3d984 commit d8d908d

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.PowerShell.EditorServices.Utility;
77
using Nito.AsyncEx;
88
using System;
9+
using System.Collections;
10+
using System.Globalization;
911
using System.Collections.Generic;
1012
using System.Collections.ObjectModel;
1113
using System.Linq;
@@ -15,7 +17,6 @@
1517

1618
namespace Microsoft.PowerShell.EditorServices
1719
{
18-
using System.Collections;
1920
using System.Management.Automation;
2021
using System.Management.Automation.Host;
2122
using System.Management.Automation.Runspaces;
@@ -322,10 +323,12 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
322323
}
323324
catch (RuntimeException e)
324325
{
325-
// TODO: Return an error
326326
Logger.Write(
327327
LogLevel.Error,
328-
"Exception occurred while attempting to execute command:\r\n\r\n" + e.ToString());
328+
"Runtime exception occurred while executing command:\r\n\r\n" + e.ToString());
329+
330+
// Write the error to the host
331+
this.WriteExceptionToHost(e);
329332
}
330333
}
331334

@@ -522,6 +525,75 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e
522525

523526
#region Private Methods
524527

528+
private void WriteExceptionToHost(Exception e)
529+
{
530+
const string ExceptionFormat =
531+
"{0}\r\n{1}\r\n + CategoryInfo : {2}\r\n + FullyQualifiedErrorId : {3}";
532+
533+
IContainsErrorRecord containsErrorRecord = e as IContainsErrorRecord;
534+
535+
if (containsErrorRecord == null ||
536+
containsErrorRecord.ErrorRecord == null)
537+
{
538+
this.WriteError(e.Message, null, 0, 0);
539+
return;
540+
}
541+
542+
ErrorRecord errorRecord = containsErrorRecord.ErrorRecord;
543+
if (errorRecord.InvocationInfo == null)
544+
{
545+
this.WriteError(errorRecord.ToString(), String.Empty, 0, 0);
546+
return;
547+
}
548+
549+
string errorRecordString = errorRecord.ToString();
550+
if ((errorRecord.InvocationInfo.PositionMessage != null) &&
551+
errorRecordString.IndexOf(errorRecord.InvocationInfo.PositionMessage, StringComparison.Ordinal) != -1)
552+
{
553+
this.WriteError(errorRecordString);
554+
return;
555+
}
556+
557+
string message =
558+
string.Format(
559+
CultureInfo.InvariantCulture,
560+
ExceptionFormat,
561+
errorRecord.ToString(),
562+
errorRecord.InvocationInfo.PositionMessage,
563+
errorRecord.CategoryInfo,
564+
errorRecord.FullyQualifiedErrorId);
565+
566+
this.WriteError(message);
567+
}
568+
569+
private void WriteError(
570+
string errorMessage,
571+
string filePath,
572+
int lineNumber,
573+
int columnNumber)
574+
{
575+
const string ErrorLocationFormat = "At {0}:{1} char:{2}";
576+
577+
this.WriteError(
578+
errorMessage +
579+
Environment.NewLine +
580+
string.Format(
581+
ErrorLocationFormat,
582+
String.IsNullOrEmpty(filePath) ? "line" : filePath,
583+
lineNumber,
584+
columnNumber));
585+
}
586+
587+
private void WriteError(string errorMessage)
588+
{
589+
((IConsoleHost)this).WriteOutput(
590+
errorMessage,
591+
true,
592+
OutputType.Error,
593+
ConsoleColor.Red,
594+
ConsoleColor.Black);
595+
}
596+
525597
void powerShell_InvocationStateChanged(object sender, PSInvocationStateChangedEventArgs e)
526598
{
527599
SessionStateChangedEventArgs eventArgs = TranslateInvocationStateInfo(e.InvocationStateInfo);

0 commit comments

Comments
 (0)