PowerLine icon indicating copy to clipboard operation
PowerLine copied to clipboard

"Exception thrown from prompt block" even when the exception is handled within the block

Open Lockszmith-GH opened this issue 4 years ago • 5 comments
trafficstars

Run the following to test this issue:

$Prompt.Add( { try{throw "error"}catch{} } )

The result is a WARNING:

WARNING: Exception thrown from prompt block. Check $PromptErrors. To suppress this message, Set-PowerLine -HideError
WARNING: Exception in PowerLinePrompt

$PromptErrors contains:

Name  : 3 { try{throw "error"}catch{} }
Value : {System.Management.Automation.RuntimeException}
  • PowerLine Module Version 3.4.0
  • Pansies Module Version 2.3.1 Tested on:
  • Arch Linux with PowerShell Core 7.1.5
  • Windows 10 with PowerShell 5.1.19041.1237

Expected behavior:

  • When an exception is captured, the error should not be displayed.

Lockszmith-GH avatar Nov 11 '21 21:11 Lockszmith-GH

Thought it might do with capturing of the error stream, but this caused the same behavior:

$Prompt.Add( { $(try{throw "error"}catch{} *>&1)} )

Lockszmith-GH avatar Nov 12 '21 16:11 Lockszmith-GH

You'll have to Set-PowerLinePrompt -HideErrors

PowerShell doesn't care that you're handling the error. I filed https://github.com/PowerShell/PowerShell/issues/3768 about this years ago.

Basically, we're always invoking your scriptblock within a script cmdlet with -ErrorVariable, something like this:

. { [CmdletBinding()]param()
 try { throw "error" } catch {}
} -ErrorVariable logging

It doesn't matter that you handled the exception -- it still shows up in the ErrorVariable, and there's nothing I can do about it except ignore all the errors (the way I do, if you set -HideErrors true).

Jaykul avatar Dec 17 '21 04:12 Jaykul

How about a way to ignore the error for a specific variable - base on output - or maybe even adding a custom object to the ErrorVariable?

I understand it's hacky as hell, but then again, sometimes a hack makes sense.

Lockszmith-GH avatar Dec 20 '21 23:12 Lockszmith-GH

PowerShell doesn't care that you're handling the error. I filed PowerShell/PowerShell#3768 about this years ago.

Thanks for that.

One bizarre thread, a lively discussion, with an agreement from the PowerShell-committe throughout the course of a few month back in 2017.

Almost implemented in 2019, accepted by the committee once more in 2020, only to be dropped because of an overworked engineer.

Is this like this all the time? asking as you've been 'around the block' more than me.

Lockszmith-GH avatar Dec 20 '21 23:12 Lockszmith-GH

I wonder if there are updates on this topic, but as it is a pretty complex one to implement, I doubt...

apetitjean avatar Nov 24 '23 07:11 apetitjean