question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an overload to System.Management.Automation.Cmdlet.WriteError that accepts an System.Exception

See original GitHub issue

During the runtime of a advanced Function the function developer may use .NET classes which produce .NET exceptions.

To rethrow an Exception in an advanced Function we can use Write-Error, Throw or $PSCmdlet.WriteError().

To set the advanced Function as origin of the Error AND to make the .NET Exception an ErrorRecord Object $PSCmdlet.WriteError() is the best candidate.

currently the user has to make an cumbersome switch inside the Catch block to catch .NET Exception Types and ErrorRecord Types.

Function Get-ErrorHandling {

    [CmdletBinding()]
    Param()

    Process {
       Try {
           
           $File = Get-Item 'C:\no\such.txt' -ErrorAction 'Stop'
                      
           [double]::Parse('Hello')
             
       } Catch {

           If($_ -is [System.Management.Automation.ErrorRecord]) {
              $PSCmdlet.WriteError($_)
           } Else {
              $PSCmdlet.WriteError((New-Object System.Management.Automation.ErrorRecord -ArgumentList $_))
           }
       }
    }
}

So $PSCmdlet.WriteError() needs an overload that accepts a .NET Exception and makes it into an ErrorRecord with an InvocationInfo of the advanced the Function here Named “Get-ErrorHandling” as source.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
SeeminglySciencecommented, Oct 9, 2019

Yeah I know, the point I was trying (poorly) to make was that those details are lost (or rather replaced with meaningless defaults). If such an overload was added, it could lead to a significant amount of first time binary module writers skipping all of those details completely.

In general my opinion is that if you want to emit a “proper” error (one that reflects the users context) then those details should be specified. I would like to see an overload like WriteError(Exception,string,ErrorCategory,object) that just creates the ErrorRecord for you while still requiring the information.

Also I don’t really see a problem with adding a switch to Write-Error to change context.

2reactions
vexx32commented, Oct 8, 2019

As far as I know, the only time you’ll get a raw exception is if there’s a parse error in the PowerShell script itself; i.e., PowerShell’s own language parser throws the exception. In most cases this won’t surface, but if it does I believe ParseExceptions have an ErrorRecord property that you can make use of for a bit of an easier time with this kind of case. 🙂

Do you know of any reproducible examples that produce an Exception object within the catch block?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cmdlet.WriteError(ErrorRecord) Method
The pipeline has already been terminated, or was terminated during the execution of this method. The Cmdlet should generally just allow PipelineStoppedException ......
Read more >
How to properly write a C# error to powershell
1 Answer 1 · non-terminating errors are reported via the System.Management.Automation.Cmdlet.WriteError method · (statement-)terminating errors ...
Read more >
[SOLVED] Powershell Error - Cannot find an overload for...
This tells you that the particular API accepts 2 overloads. First, a string, being the "deviceExternalID". And Second, a DeviceValue[] object ...
Read more >
Net methods that throw bad errors.
After much questing, I have found how to query Access databases in Powershell. Code: $conn = new-object data.odbc.odbcconnection $conn.
Read more >
PowerShell basics - Notes from a DevSecOps consultant
One of the first questions people have when they begin to use PowerShell for scripting is how to manipulate the output from a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found