-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
Writing to the error stream produces no output in methods of custom classes #9702
Comments
Write-Error doesn't work inside method, only throw works (this is maybe by design)
Set ErrorAction to Stop, throws the error ( and you can use try/catch to handle the error)
|
Indeed, the use of |
@fMichaleczek: Yes, terminating errors work, because they abort execution of the method, but my concern was about nonterminating errors that you may simply want to pass through - while continuing to execute the method. @vexx32, I naively thought that given that you call cmdlets inside methods, their nonterminating errors would surface too (e.g., |
I'd be more inclined to call it a bug that they leak through when the return type is
I think if you really want a class to emit error records or any other stream, you should pass a |
So you think custom classes are their own world that deals in return values only that then map onto the success output stream - with no other streams available? Certainly would require documentation, so that no one expects it to work differently, they way I did (happy to create an issue once we have a shared understanding). However, the worlds are not separate; the error stream is the only exception:
PS> class Foo { [string] Bar() { Get-Item /NoSuch; write-warning warning; write-verbose verbose -vb; write-debug -debug debug; write-information -InformationAction continue information; return 'hi' } }; $Error.Clear(); [Foo]::new().Bar()
WARNING: warning
VERBOSE: verbose
DEBUG: debug
information
hi |
In my opinion, yeah absolutely.
Well, yes and no. Something that I've never liked about those cmdlets is they work in the context of the Just so I'm clear though, I'm not pointing at any of that as proof that it should be one way or another, it's just implementation detail. I don't have any insight on what the PowerShell team intended, this is just my opinion. |
That's helpful background information, @SeeminglyScience, thanks. @SteveL-MSFT, can you shed light on the design intent and suggest a resolution? |
@mklement0 The design intent was that PowerShell classes should have semantics equivalent to .NET classes (since they are, in fact, .NET classes.) This means explicit return types, a requirement to use the return statement, variables must be initialized before being used, detected at compile-time, not run time, etc. The goal with classes was to make it possible to write more reliable (and larger) scripts in PowerShell by providing more conventional programming language semantics. |
Thanks, @bpayette, but what I want to know more specifically is the design intent with respect to passing the PowerShell output streams through (except the success output stream) when commands are called from inside custom-class methods. |
The intent was that streams are not a part of classes semantically speaking. Methods return values and throw errors. |
A somewhat tangential question, @bpayette, then: what of the other streams? It seems the information stream is handled well enough, and though I'll have to test this evening I am fairly sure I recall verbose and debug streams also behaving as they would outside a class context. |
Indeed, @vexx32: as the code in the comment above demonstrates, streams 3 - 6 are passed through. |
@vexx32 Those streams are neither output nor error hence they are not involved in a discussion of output/error semantics. |
How unfortunate, then, that they are involved in the implementation - which led to their involvement in this discussion. I guess documenting this half-blending/separation of the worlds is our best option at this point: MicrosoftDocs/PowerShell-Docs#4497 |
Write-Error is (almost) a no-op inside methods. It used to work inside void methods in older versions of PowerShell but it was a bug, not an intentional feature. See PowerShell/PowerShell#5331 and PowerShell/PowerShell#9702 Also, "throw" is more correct semantically.
Write-Error is (almost) a no-op inside methods. It currently works inside void methods but it's a bug, not an intentional feature. See PowerShell/PowerShell#5331 and PowerShell/PowerShell#9702 Also, "throw" is more correct semantically.
Write-Error is (almost) a no-op inside methods. It currently works inside void methods but it's a bug, not an intentional feature. See PowerShell/PowerShell#5331 and PowerShell/PowerShell#9702 Also, "throw" is more correct semantically.
Note: Only
[void]
-typed methods do not exhibit the problem demonstrated below.Steps to reproduce
Expected behavior
Actual behavior
That is, the error stream output was quietly suppressed.
However, the error is recorded in the automatic
$Error
variable.Update: By contrast, all other streams (warning, verbose, debug, information) are passed through.
Environment data
The text was updated successfully, but these errors were encountered: