-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Implement Get-Error cmdlet as Experimental Feature
#10727
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
Merged
adityapatwardhan
merged 23 commits into
PowerShell:master
from
SteveL-MSFT:resolve-errorrecord
Oct 15, 2019
Merged
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 73ed163
add formatting
SteveL-MSFT e1d667a
use DebugForegroundColor instead of VerboseForegroundColor
SteveL-MSFT 05c842d
make TargetSite use same indentation as other nested types
SteveL-MSFT 2c77798
fix extra newline for multi-strings (like stacktrace)
SteveL-MSFT 2f51f23
make it an experimental feature
SteveL-MSFT f545e73
add new FormatAccentColor
SteveL-MSFT 033624a
add tests
SteveL-MSFT 2926591
add check that Exp Feature is enabled for tests
SteveL-MSFT f74d6ee
fix so ParserExceptions are handled correctly
SteveL-MSFT 745c423
address most CodeFactor issues, but ignoring a few for consistency
SteveL-MSFT 35f0109
fix default command test
SteveL-MSFT 84d9bae
fix default commands test
SteveL-MSFT 3251110
left justify stack traces
SteveL-MSFT ceb3fc2
address Aditya's feedback
SteveL-MSFT e362539
increase test value as Linux and macOS don't seem to recycle pwsh so …
SteveL-MSFT 56cee13
fix test condition
SteveL-MSFT a2fdc53
update cmdlet name to Get-Error
SteveL-MSFT 77d0dc7
address Jim's feedback, also fix indentation of nested objects to 4 s…
SteveL-MSFT 4704887
change to use array for specific types to recurse into and special ha…
SteveL-MSFT 7228910
show Authorization header as ellipsis instead of hiding
SteveL-MSFT a3e0c52
fix rendering of parser errors
SteveL-MSFT 36c87e6
address Kirk's feedback and suppress `Data` of type Dictionary to be …
SteveL-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/Microsoft.PowerShell.Commands.Utility/commands/utility/Get-Error.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
SteveL-MSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 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)] | ||
SteveL-MSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; } | ||
SteveL-MSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// <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"); | ||
SteveL-MSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| obj.TypeNames.Remove("System.Exception"); | ||
|
|
||
| if (addErrorIdentifier) | ||
| { | ||
| obj.Properties.Add(new PSNoteProperty("PSErrorIndex", index++)); | ||
| } | ||
|
|
||
| WriteObject(obj); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.