-
Notifications
You must be signed in to change notification settings - Fork 237
Make sure ErrorRecords go to Error stream #1201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -41,17 +41,28 @@ public class PowerShellContextService : IDisposable, IHostSupportsInteractiveSes | |||||
"../../Commands/PowerShellEditorServices.Commands.psd1")); | ||||||
|
||||||
private static readonly Action<Runspace, ApartmentState> s_runspaceApartmentStateSetter; | ||||||
private static readonly PropertyInfo s_writeStreamProperty; | ||||||
private static readonly object s_errorStreamValue; | ||||||
|
||||||
[SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline", Justification = "cctor needed for version specific initialization")] | ||||||
static PowerShellContextService() | ||||||
{ | ||||||
// PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection | ||||||
// PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection. | ||||||
if (!VersionUtils.IsNetCore || VersionUtils.IsPS7OrGreater) | ||||||
{ | ||||||
MethodInfo setterInfo = typeof(Runspace).GetProperty("ApartmentState").GetSetMethod(); | ||||||
Delegate setter = Delegate.CreateDelegate(typeof(Action<Runspace, ApartmentState>), firstArgument: null, method: setterInfo); | ||||||
s_runspaceApartmentStateSetter = (Action<Runspace, ApartmentState>)setter; | ||||||
} | ||||||
|
||||||
if (VersionUtils.IsPS7OrGreater) | ||||||
{ | ||||||
// Used to write ErrorRecords to the Error stream. Using Public and NonPublic because the plan is to make this property | ||||||
// public in 7.0.1 | ||||||
s_writeStreamProperty = typeof(PSObject).GetProperty("WriteStream", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); | ||||||
Type writeStreamType = typeof(PSObject).Assembly.GetType("System.Management.Automation.WriteStreamType"); | ||||||
s_errorStreamValue = Enum.Parse(writeStreamType, "Error"); | ||||||
} | ||||||
} | ||||||
|
||||||
#region Fields | ||||||
|
@@ -1924,43 +1935,22 @@ internal void WriteOutput( | |||||
} | ||||||
} | ||||||
|
||||||
private void WriteExceptionToHost(Exception e) | ||||||
private void WriteExceptionToHost(RuntimeException e) | ||||||
{ | ||||||
const string ExceptionFormat = | ||||||
"{0}\r\n{1}\r\n + CategoryInfo : {2}\r\n + FullyQualifiedErrorId : {3}"; | ||||||
|
||||||
if (!(e is IContainsErrorRecord containsErrorRecord) || | ||||||
containsErrorRecord.ErrorRecord == null) | ||||||
{ | ||||||
this.WriteError(e.Message, null, 0, 0); | ||||||
return; | ||||||
} | ||||||
var psObject = PSObject.AsPSObject(e.ErrorRecord); | ||||||
|
||||||
ErrorRecord errorRecord = containsErrorRecord.ErrorRecord; | ||||||
if (errorRecord.InvocationInfo == null) | ||||||
// Used to write ErrorRecords to the Error stream so they are rendered in the console correctly. | ||||||
if (VersionUtils.IsPS7OrGreater) | ||||||
{ | ||||||
this.WriteError(errorRecord.ToString(), String.Empty, 0, 0); | ||||||
return; | ||||||
s_writeStreamProperty.SetValue(psObject, s_errorStreamValue); | ||||||
} | ||||||
|
||||||
string errorRecordString = errorRecord.ToString(); | ||||||
if ((errorRecord.InvocationInfo.PositionMessage != null) && | ||||||
errorRecordString.IndexOf(errorRecord.InvocationInfo.PositionMessage, StringComparison.Ordinal) != -1) | ||||||
else | ||||||
{ | ||||||
this.WriteError(errorRecordString); | ||||||
return; | ||||||
var note = new PSNoteProperty("writeErrorStream", true); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got that from PowerShell but since it seems to be case insensitive I can make this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May as well use what PowerShell uses. Strange they did it that way |
||||||
psObject.Properties.Add(note); | ||||||
} | ||||||
|
||||||
string message = | ||||||
string.Format( | ||||||
CultureInfo.InvariantCulture, | ||||||
ExceptionFormat, | ||||||
errorRecord.ToString(), | ||||||
errorRecord.InvocationInfo.PositionMessage, | ||||||
errorRecord.CategoryInfo, | ||||||
errorRecord.FullyQualifiedErrorId); | ||||||
|
||||||
this.WriteError(message); | ||||||
ExecuteCommandAsync(new PSCommand().AddCommand("Microsoft.PowerShell.Core\\Out-Default").AddParameter("InputObject", psObject)); | ||||||
} | ||||||
|
||||||
private void WriteError( | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.