Closed
Description
Ubiquitous -OnError {Scriptblock} parameter which would take precedence over -ErrorAction and others. The scriptbock would be called on terminating errors and would be called for every non-terminating error. For non-terminating errors, the scriptblock can control whether to STOP or CONTINUE code execution.
This would unify the processing of terminating vs non terminating errors:
# Nonterminating error
Stop-Process foo* -OnError {
Write-Host "Had an error."
}
# Terminating error
Start-CmdwithTerminatingError -OnError {
Write-Host "Had an error."
}
# Terminating errors always terminate but now non-terminating errors are controlled by Continue/Break
# Continue == -ErrorAction Continue
# Break == -ErrorAction Stop
# -ErrorAction Debug can be achieved by writing a message, doing a Wait-Debugger and then
# afterwards you can continue or break.
Stop-Process foo* -OnError {
Write-Host "Had an error. Continuing"
Continue
}
Stop-Process foo* -OnError {
Write-Host "Had an error. Stopping"
Break
}
Stop-Process foo* -OnError {
Write-Host "Had an error. Debug this script"
Wait-Debugger
if ((Read-Host "Type 'y' to continue") -eq 'y'){
Continue
} else {
Break
}
}
Start-CmdwithTerminatingError -OnError {
Write-Host "Had an error. Stopping or Continuing has the same effect"
Break
}
# scriptblock is run at the botton - you have the full call stack preserved
stop-process foo* -OnError {
Write-Host "PSCALLstack is preserved!"
Get-PSCallstack
}
# The exception is surfaced through $_ the same as with try/catch
Stop-Process foo* -OnError {
Write-Host "Error stopping: $($_.TargetObject)`n"
}