It would be good to have the ability to drop into the script debugger on error. This is relatively easy for non-terminating errors since the PowerShell engine continues to run and so the debugger runs. But exceptions are trickier since the process state is unknown. For exceptions thrown by PowerShell (ErrorAction: "Stop") we could stop in the debugger before throwing the exception. It might be possible to have an unhandled exception handler to allow inspection of the runspace on a thrown exception.
I'd love to wire such a feature up to Visual Studio Code's exception breakpoints.
Consider exposing this as set-psbreakpoint -breakonerror
Seems like you could install an unhandled exception filter to catch the exception during the first pass - in which case, process state should still be known. I particularly like this old Matt Pietrek article on the subject - https://www.microsoft.com/msj/0197/Exception/Exception.aspx
@SteveL-MSFT Agreed.
@rkeithhill It turns out we already have this in the expression IL for exception handling routing (try/catch, trap) and we can use this to hook into the debugger. Still lots of details to work out but this looks very doable.
Thanks to @lzybkr for pointing this out.
@SteveL-MSFT alternative:
Debug-Runspace -BreakOnError
Set-RunspaceDebug -BreakOnError
EDIT: Moving my comments and vscode workaround to https://github.com/PowerShell/vscode-powershell/issues/298
This has been implemented by Kirk as an experimental feature.
Most helpful comment
@SteveL-MSFT alternative:
Debug-Runspace -BreakOnErrorSet-RunspaceDebug -BreakOnError