Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
05e08d0
add Resolve-ErrorRecord cmdlet
SteveL-MSFT Oct 5, 2019
73ed163
add formatting
SteveL-MSFT Oct 7, 2019
e1d667a
use DebugForegroundColor instead of VerboseForegroundColor
SteveL-MSFT Oct 7, 2019
05c842d
make TargetSite use same indentation as other nested types
SteveL-MSFT Oct 7, 2019
2c77798
fix extra newline for multi-strings (like stacktrace)
SteveL-MSFT Oct 7, 2019
2f51f23
make it an experimental feature
SteveL-MSFT Oct 7, 2019
f545e73
add new FormatAccentColor
SteveL-MSFT Oct 7, 2019
033624a
add tests
SteveL-MSFT Oct 8, 2019
2926591
add check that Exp Feature is enabled for tests
SteveL-MSFT Oct 8, 2019
f74d6ee
fix so ParserExceptions are handled correctly
SteveL-MSFT Oct 8, 2019
745c423
address most CodeFactor issues, but ignoring a few for consistency
SteveL-MSFT Oct 8, 2019
35f0109
fix default command test
SteveL-MSFT Oct 8, 2019
84d9bae
fix default commands test
SteveL-MSFT Oct 8, 2019
3251110
left justify stack traces
SteveL-MSFT Oct 9, 2019
ceb3fc2
address Aditya's feedback
SteveL-MSFT Oct 11, 2019
e362539
increase test value as Linux and macOS don't seem to recycle pwsh so …
SteveL-MSFT Oct 11, 2019
56cee13
fix test condition
SteveL-MSFT Oct 11, 2019
a2fdc53
update cmdlet name to Get-Error
SteveL-MSFT Oct 14, 2019
77d0dc7
address Jim's feedback, also fix indentation of nested objects to 4 s…
SteveL-MSFT Oct 15, 2019
4704887
change to use array for specific types to recurse into and special ha…
SteveL-MSFT Oct 15, 2019
7228910
show Authorization header as ellipsis instead of hiding
SteveL-MSFT Oct 15, 2019
a3e0c52
fix rendering of parser errors
SteveL-MSFT Oct 15, 2019
36c87e6
address Kirk's feedback and suppress `Data` of type Dictionary to be …
SteveL-MSFT Oct 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class for Get-Error implementation.
/// </summary>
[Experimental("Microsoft.PowerShell.Utility.PSGetError", ExperimentAction.Show)]
[Cmdlet(VerbsCommon.Get, "Error",
HelpUri = "https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/get-error?view=powershell-7&WT.mc_id=ps-gethelp",
DefaultParameterSetName = NewestParameterSetName)]
public sealed class GetErrorCommand : PSCmdlet
{
internal const string ErrorParameterSetName = "Error";
internal const string NewestParameterSetName = "Newest";
internal const string AliasNewest = "Last";

/// <summary>
/// Gets or sets the error object to resolve.
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ParameterSetName = ErrorParameterSetName)]
[ValidateNotNullOrEmpty]
public PSObject InputObject { get; set; }

/// <summary>
/// Gets or sets the number of error objects to resolve starting with newest first.
/// </summary>
[Parameter(ParameterSetName = NewestParameterSetName)]
[Alias(AliasNewest)]
[ValidateRange(1, int.MaxValue)]
public int Newest { get; set; } = 1;

/// <summary>
/// Process the error object.
/// </summary>
protected override void ProcessRecord()
{
var errorRecords = new List<object>();
var index = 0;

if (InputObject != null)
{
if (InputObject.BaseObject is Exception || InputObject.BaseObject is ErrorRecord)
{
errorRecords.Add(InputObject);
}
}
else
{
var errorVariable = SessionState.PSVariable.Get("error");
var count = Newest;
ArrayList errors = (ArrayList)errorVariable.Value;
if (count > errors.Count)
{
count = errors.Count;
}

while (count > 0)
{
errorRecords.Add(errors[index]);
index++;
count--;
}
}

index = 0;
bool addErrorIdentifier = errorRecords.Count > 1 ? true : false;

foreach (object errorRecord in errorRecords)
{
PSObject obj = PSObject.AsPSObject(errorRecord);
obj.TypeNames.Insert(0, "PSExtendedError");

// Remove some types so they don't get rendered by those formats
obj.TypeNames.Remove("System.Management.Automation.ErrorRecord");
obj.TypeNames.Remove("System.Exception");

if (addErrorIdentifier)
{
obj.Properties.Add(new PSNoteProperty("PSErrorIndex", index++));
}

WriteObject(obj);
}
}
}
}
103 changes: 81 additions & 22 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,125 +735,184 @@ public ConsoleColorProxy(ConsoleHostUserInterface ui)
_ui = ui;
}

public ConsoleColor FormatAccentColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{
return _ui.FormatAccentColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{
_ui.FormatAccentColor = value;
}
}

public ConsoleColor ErrorAccentColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ErrorAccentColor; }
{
return _ui.ErrorAccentColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ErrorAccentColor = value; }
{
_ui.ErrorAccentColor = value;
}
}

public ConsoleColor ErrorForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ErrorForegroundColor; }
{
return _ui.ErrorForegroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ErrorForegroundColor = value; }
{
_ui.ErrorForegroundColor = value;
}
}

public ConsoleColor ErrorBackgroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ErrorBackgroundColor; }
{
return _ui.ErrorBackgroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ErrorBackgroundColor = value; }
{
_ui.ErrorBackgroundColor = value;
}
}

public ConsoleColor WarningForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.WarningForegroundColor; }
{
return _ui.WarningForegroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.WarningForegroundColor = value; }
{
_ui.WarningForegroundColor = value;
}
}

public ConsoleColor WarningBackgroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.WarningBackgroundColor; }
{
return _ui.WarningBackgroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.WarningBackgroundColor = value; }
{
_ui.WarningBackgroundColor = value;
}
}

public ConsoleColor DebugForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.DebugForegroundColor; }
{
return _ui.DebugForegroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.DebugForegroundColor = value; }
{
_ui.DebugForegroundColor = value;
}
}

public ConsoleColor DebugBackgroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.DebugBackgroundColor; }
{
return _ui.DebugBackgroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.DebugBackgroundColor = value; }
{
_ui.DebugBackgroundColor = value;
}
}

public ConsoleColor VerboseForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.VerboseForegroundColor; }
{
return _ui.VerboseForegroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.VerboseForegroundColor = value; }
{
_ui.VerboseForegroundColor = value;
}
}

public ConsoleColor VerboseBackgroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.VerboseBackgroundColor; }
{
return _ui.VerboseBackgroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.VerboseBackgroundColor = value; }
{
_ui.VerboseBackgroundColor = value;
}
}

public ConsoleColor ProgressForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ProgressForegroundColor; }
{
return _ui.ProgressForegroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ProgressForegroundColor = value; }
{
_ui.ProgressForegroundColor = value;
}
}

public ConsoleColor ProgressBackgroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ProgressBackgroundColor; }
{
return _ui.ProgressBackgroundColor;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ProgressBackgroundColor = value; }
{
_ui.ProgressBackgroundColor = value;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,9 @@ public override void WriteErrorLine(string value)
}
}

// Format colors
public ConsoleColor FormatAccentColor { get; set; } = ConsoleColor.Green;

// Error colors
public ConsoleColor ErrorAccentColor { get; set; } = ConsoleColor.Cyan;
public ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.Red;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CmdletsToExport = @(
'Get-TraceSource', 'Set-TraceSource', 'Add-Type', 'Get-TypeData', 'Remove-TypeData', 'Update-TypeData',
'Get-UICulture', 'Get-Unique', 'Get-Uptime', 'Clear-Variable', 'Get-Variable', 'New-Variable',
'Remove-Variable', 'Set-Variable', 'Get-Verb', 'Write-Verbose', 'Write-Warning', 'Invoke-WebRequest',
'Format-Wide', 'ConvertTo-Xml', 'Select-Xml'
'Format-Wide', 'ConvertTo-Xml', 'Select-Xml', 'Get-Error'
)
FunctionsToExport = @()
AliasesToExport = @('fhx')
Expand All @@ -38,6 +38,10 @@ PrivateData = @{
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger.'
}
@{
Name = 'Microsoft.PowerShell.Utility.PSGetError'
Description = 'Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects'
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CmdletsToExport = @(
'ConvertFrom-StringData', 'Format-Table', 'New-TemporaryFile', 'New-TimeSpan', 'Get-TraceSource', 'Set-TraceSource',
'Add-Type', 'Get-TypeData', 'Remove-TypeData', 'Update-TypeData', 'Get-UICulture', 'Get-Unique', 'Get-Uptime',
'Clear-Variable', 'Get-Variable', 'New-Variable', 'Remove-Variable', 'Set-Variable', 'Get-Verb', 'Write-Verbose',
'Write-Warning', 'Invoke-WebRequest', 'Format-Wide', 'ConvertTo-Xml', 'Select-Xml'
'Write-Warning', 'Invoke-WebRequest', 'Format-Wide', 'ConvertTo-Xml', 'Select-Xml', 'Get-Error'
)
FunctionsToExport = @()
AliasesToExport = @('fhx')
Expand All @@ -36,6 +36,10 @@ PrivateData = @{
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger.'
}
@{
Name = 'Microsoft.PowerShell.Utility.PSGetError'
Description = 'Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects'
}
)
}
}
Expand Down
Loading