Skip to content

Promoting a statement-terminating error to a script-terminating one results in unhelpful $Error entry. #4837

Closed

Description

Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglecting to make this important distinction.

In practice, an (uncaught) statement-terminating error (e.g., 1 / 0 or Get-Item -NoSuchParam) does not terminate the script, but can be made to do so via $ErrorActionPreference = 'Stop', against documented behavior (but not with -ErrorAction Stop).

If you do, you get:

  • the usual error record reflecting the statement-terminating error in $Error
  • an additional, unhelpful $Error entry that is simply a part of the statement-terminating-error record, namely the underlying exception.

In other words: you get 2 $Error entries, with the exception-only entry coming after (reflected in $Error[0] immediately after, with $Error[1] containing the statement-terminating-error record.

The exact relationship is:

[object]::ReferenceEquals($Error[1].exception, $Error[0]) # -> $True

Contrast this with promoting a non-terminating error to a script-terminating one, where you also get 2 entries, but the extra entry is a helpful exception ([System.Management.Automation.ActionPreferenceStopException]) whose message reflects the fact of the promotion:

# Promote a non-terminating error to a script-terminating one.
> $Error.Clear(); Get-Item -ErrorAction Stop /NoSuchItem
Get-Item : Cannot find path '/NoSuchItem' because it does not exist.
...
# Print the message of the extra $Error entry that was recorded.
# (exception System.Management.Automation.ActionPreferenceStopException)
> $Error[0].Message
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find path '/NoSuchItem' because it does not exist.

Steps to reproduce

$Error.Clear(); $ErrorActionPreference = 'stop'; Get-Item -NoSuchSwitch
'---'
$Error | % GetType | % Name

Expected behavior

I'm describing expected behavior from the perspective of consistency with how things work with non-terminating errors:

Get-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.
...
---
ActionPreferenceStopException  # indicate promotion to script-terminating error
ErrorRecord

Actual behavior

Get-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.
...
---
ParameterBindingException # !! Simply duplicated the .Exception value of $Error[1]
ErrorRecord

Environment data

PowerShell Core v6.0.0-beta.6 on macOS 10.12.6
PowerShell Core v6.0.0-beta.6 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.6 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.502 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. added
    WG-Engine
    on Sep 15, 2017
  2. KirkMunro commented on Apr 22, 2019

    @KirkMunro
    Contributor

    Wouldn't a more appropriate expected behavior for this be a single entry in $error consisting of the ErrorRecord that identifies the error, with the Exception property referencing an ActionPreferenceStopException that has an InnerException of ParameterBindingException?

    i.e. Why are we generating two entries in $error for what an end user perceives as a single error?

    Similarly, when you have a ParseException, why are we adding an exception of type ParseException that contains an ErrorRecord with an Exception property of type ParentContainsErrorRecordException to $error? Why don't we simply add an ErrorRecord that contains an exception of type ParseException instead?

    In either case, these changes could possibly be breaking for anyone who had built tooling that processes $error or that performs error handling, so does it matter? I'm not asking that because I feel these changes should not be made -- the fact that raw exceptions are added to $error and they way they are added makes it more difficult to process errors since the contents of the collection are inconsistent -- but I do wonder if it's worth my time to go through and try to correct such issues (which I would do, because I'm spending a lot of time with the PowerShell parser and debugger right now). I suppose an RFC is in order to discuss the collection of issues and propose a fix that would be beneficial enough to implement.

  3. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  5. added
    Resolution-No Activity
    on Nov 16, 2023
  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions