Powershell: PS7: Get-Error should report the fully-qualified error name

Created on 18 Nov 2019  Â·  7Comments  Â·  Source: PowerShell/PowerShell

Summary of the new feature/enhancement


Get-Error reports fullyqualifiederrorid without its namespace. This can make it difficult to find the fully qualified exception for use in catch statements, especially if running in Azure Functions or other environments with custom exceptions and you only have text logs to go off of. Since the major point of Get-Error is to address exceptions and perhaps catch them, the fully qualified type is important to do a specific catch, as my most common workflow when looking at errors is doing $error[0].exception.gettype().fullname as the very next step so I know what to use in a try/catch.

Proposed technical implementation details (optional)

Add FullyQualifiedErrorType and FullyQualifiedErrorParentType properties that would display the full error type (what you would submit as a type to catch) as well as other error types that would catch this property, minus the obvious "system.exception" and "system.object" that all exceptions inherit from.

e.g.
FullyQualifiedErrorType:

$error[0].exception.gettype()

FullyQualifiedErrorParentTypes:

$currentType = $error[0].exception.getType()
while ($CurrentType) {
    $currentType = $currentType.BaseType
    if ($currentType.fullname -eq 'System.Exception') {break}
    $currentType
}
Area-Cmdlets-Utility Issue-Enhancement Resolution-Fixed

Most helpful comment

I believe https://github.com/PowerShell/PowerShell/pull/11076 already resolves this? Didn't make it in time for Preview.6, but should be there for RC.

All 7 comments

/cc @SteveL-MSFT

Note that .FullyQualifiedErrorId is _not_ the same as the [type name of] the exception underlying the error record; when you create an error record, you can choose an error ID at your discretion; e.g.:

PS> [System.Management.Automation.ErrorRecord]::new([exception]::new(), 'MyErrorId', 'InvalidData', $null) | Get-Error

Exception             :
    Message : Exception of type 'System.Exception' was thrown.
    HResult : -2146233088
CategoryInfo          : InvalidData: (:) [], Exception
FullyQualifiedErrorId : MyErrorId

Note how the FullyQualifiedErrorId: line reflects the custom error ID passed to the error-record constructor - MyErrorId - as-is.

However, I agree that it _is_ desirable to have the full exception type name reflected in the output, by introducing _another output line_, e.g., Type::

Exception             :
    Type: System.Exception   # <<<< new line with full type name.
    Message : Exception of type 'System.Exception' was thrown.
    HResult : -2146233088
CategoryInfo          : InvalidData: (:) [], Exception
FullyQualifiedErrorId : MyErrorId

Ditto for .InnerException.

@SteveL-MSFT: Not sure if more work will be done before v7 GA; note that the VT sequences in Get-Error's output are currently invariably emitted, even when you capture the output in a file.

Right, my point was simply that there should be a property that at least
contains either the type definition of the exception or its string
representation, since 80% of the time, that's what I'm looking for.

On Tue, Nov 19, 2019, 9:55 AM Michael Klement notifications@github.com
wrote:

Note that .FullyQualifiedErrorId is not the same as the [type name of]
the exception underlying the error record; when you create an error record,
you can choose an error ID at your discretion; e.g.:

PS> [System.Management.Automation.ErrorRecord]::new([exception]::new(), 'MyErrorId', 'InvalidData', $null) | Get-Error

Exception :
Message : Exception of type 'System.Exception' was thrown.
HResult : -2146233088
CategoryInfo : InvalidData: (:) [], Exception
FullyQualifiedErrorId : MyErrorId

Note how the FullyQualifiedErrorId: line reflects the custom error ID
passed to the error-record constructor - MyErrorId - as-is.

However, I agree that it is desirable to have the full exception type
name reflected in the output, by introducing another output line, e.g., Type:
:

Exception :
Type: System.Exception # <<<< new line with full type name.
Message : Exception of type 'System.Exception' was thrown.
HResult : -2146233088
CategoryInfo : InvalidData: (:) [], Exception
FullyQualifiedErrorId : MyErrorId

Ditto for .InnerException.

@SteveL-MSFT https://github.com/SteveL-MSFT: Not sure if more work will
be done before v7 GA; note that the VT sequences in Get-Error's output
are currently invariably emitted, even when you capture the output in a
file.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/PowerShell/PowerShell/issues/11106?email_source=notifications&email_token=ADUNKUSREBXX63SOZWVJZB3QUQR75A5CNFSM4JO2FVD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEPDRPA#issuecomment-555628732,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ADUNKUWOJOVOZLYGVIPM6PDQUQR75ANCNFSM4JO2FVDQ
.

I believe https://github.com/PowerShell/PowerShell/pull/11076 already resolves this? Didn't make it in time for Preview.6, but should be there for RC.

Great, @SteveL-MSFT; yes, that should resolve it.

However, can I suggest using just Type: rather than ExceptionType: as the label?
This would be consistent with the other labels; e.g., it's Message:, not ExceptionMessage:.

@JustinGrote, can you please close this issue?

Totally missed the PR (there's a lot of them after all). Thanks @SteveL-MSFT. Meets my criteria.

Fixed by #11076

@SteveL-MSFT, while you're working on #11076, can you look at these new issues?

  • #11121
  • #11122
  • #11123
Was this page helpful?
0 / 5 - 0 ratings