-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ubiquitous -OnError {ScriptBlock} parameter #6010
Comments
|
Really like the idea as there isn't really any good way to handle errors from the error stream. The only way known to me is something like this: Get-Whatever 2>&1 |
ForEach-Object {
if ($_ -is [ErrorRecord]) {
# handle error
} else {
$_
}
}However, the |
|
So, how does it work for distinguishing the error type and acting accordingly? Let's say there is a script in which a file has to be deleted. The Remove-Item command might end with an error when:
|
|
You would presumably get |
|
Just to call it out, PR #8205 is currently in the review process and should be in place in PowerShell 7 shortly (it's just waiting for PR #9825 to finish review and be merged so that automated scripts can be written against the debugger). It adds an Stop-Process foo* -ErrorAction BreakThat command will automatically enter the debugger on terminating or non-terminating error, and from that point you can check the call stack, step, continue execution, or terminate (quit) the script that is running. Once that is merged, it will resolves some of the needs identified here (breaking into the debugger and/or checking the callstack on error). |
|
FYI, this RFC proposes a different approach that I believe provides a better method for doing this, for all streams, without introducing a bunch of new common variables: PowerShell/PowerShell-RFC#219. It supercedes the closed RFC196 proposal that is referenced above. |
|
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
1 similar comment
|
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
|
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
|
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

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:
The text was updated successfully, but these errors were encountered: