Replies: 26 comments
-
Spoiler: I think this is wrong.Here is the clearest, most obvious counter-example I can think of right now: Get-AzureKeyVaultSecret I use this command because it's one that has a few easy to understand use cases and specific issues that lead to a specific design (in my opinion). It has a few parameter sets:
In each case, if it doesn't find a matching value, it quietly returns nothing. I consider that to be the best possible design, because:
However, any time you're looking up an item by name, it's very likely that the value doesn't exist (yet). If the command produced an error for the "everything worked fine, but there's nothing to return" case, I believe it would be extremely complicated to use correctly, and would generally result in careless or inexperienced users hiding the important errors. Error handling for these cases is terribleThe first time a user calls the command and it returns a trivial "not found" error, they might choose to suppress that error by adding A design which produces errors for missing values forces careful users to capture the error and check which error it was. In fact ... if the error is not terminating, it's even worse than if it were terminating like the others.In order to suppress a non-terminating error, you must use
However, even if the command simply throws a different unique exception for each use case (or writes an error with a different unique error category), producing an error in this situation results in the user needing to explicitly handle the error or avoid it ... In order to allow you to avoid the error, the PowerShell Committee's proposal essentially requires every For expensive commands, I'm afraid this this is a terrible ideaIf The problem is that the Worse yet, you're not just making TWO requests when you could just make one -- it's actually the exact same request each time, because there's no REST method for "test" so you would be calling the |
Beta Was this translation helpful? Give feedback.
-
On phone so employing brevity. I strongly disagree that there should be an error. I see Get-ChildItem EmptyFolder As Give me all of the contents of the folder. If there aren't any don't return anything. It's not an error if it is doing what I ask. I also think it is misleading as "an error" means something is wrong. Noth in ng to return is not "wrong" |
Beta Was this translation helpful? Give feedback.
-
I strongly agree with Joel. Returning nothing when there is nothing to return is the appropriate response and not an error. |
Beta Was this translation helpful? Give feedback.
-
I don't think
Well,
And I think rightly so. CMD and Bash also error in this case. And while we're looking at these examples ... RE 1, this is a filtering scenario therefore it is behaving as designed - to not return a non-term error. Personally I'm not big fan of these "inherently filtering" commands. If I wanted filtering I would just specify RE 2, doesn't error now and wouldn't error if implemented under the proposal IMO. The use of RE 5, it would have been nice if this generated a non-terminating error IMO. Again would have been easy to avoid with Back to the original question:
IMO you error when you don't have the specific item I asked for and your design is to only return a single item i.e. not all things that match the term I specified. But please, require a wildcard char for filtering - that's what they're for. However, there are nuances here, I'll give you that. Like asking for the contents of a specific (empty) folder should not generate an error. Or asking for the "contents" of a specific (empty) file should not generate an error either. |
Beta Was this translation helpful? Give feedback.
-
Just because a command writes non-terminating errors doesn't mean it can't also throw terminating errors. A connectivity problem should result in a terminating error since there's no way the command can proceed. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@rkeithhill but you've ignored my argument in favor of picking apart the op examples. let's set all of that aside. Read my first reply, and explain to me how your logic applies to However, this comment seems like a HUGE misunderstanding, which in my opinion is the root cause of your point of view (and everyone who thinks these should error):
That's simply not reasonable. Take this sample command: function Test-Error {
[CmdletBinding()]
param(
[switch]$Exception,
[switch]$Error
)
if($Error){
Write-Error "Test Error" -Category SecurityError
}
if($Exception) {
throw ([Exception]::new("Syntax Problem"))
}
} Are you saying that the user should just learn to ignore and live with the huge block of red from |
Beta Was this translation helpful? Give feedback.
-
Yup:
Or if you prefer:
@SQLDBAWithABeard then you should be disappointed in a lot of PowerShell |
Beta Was this translation helpful? Give feedback.
-
I'm on board with what @Jaykul wrote up top. Tweet-sized:
The existing commands that follow the error-if-nothing-found convention are painful to deal with. I don't understand why this is even up for debate. Do folks seriously want to create a common convention forcing everyone who writes PowerShell to handle multiple error types (which differ for each command, generally) for something that should (and often is, currently) as simple as testing for presence of a value? I realize some folks here are very experienced, but this seems like a bad idea that would affect the usability and desirability of the language for a large swath of folks, including myself. |
Beta Was this translation helpful? Give feedback.
-
I wonder if it is just the "sea of red" that so many folks have an aversion to that they're willing to not have commands error just to avoid it. I can certainly sympathize. We've tried in the past to argue for more succinct error msgs. I would love to see something like this:
What I don't like about that is that the $error collection is a stack and that error index above of
where
Also, with Get-Error, I'd like to see a
|
Beta Was this translation helpful? Give feedback.
-
Yup. Although the "forcing everyone" bit sounds ominous. Did you see that list I posted above? You already have to do this with many Also folks seem to be only considering the scripting case and not the interactive CLI case. I happen to believe that most folks using a shell expect |
Beta Was this translation helpful? Give feedback.
-
@rkeithhill or others who are in the |
Beta Was this translation helpful? Give feedback.
-
Given that the system temp dir is
If this command were to return nothing, I would assume the directory is empty and not that it actually doesn't exist. That's not a good user experience. |
Beta Was this translation helpful? Give feedback.
-
Yes - when considering this subject. Although I can work around it when coding or when at the command line I can read it. I have to explain it to many people who are intimidated by the sea of red. I always want PowerShell to be as easy as possible for all people If there is a chance to influence the choice, my choice is for no error if nothing to return
I think this is a poor example @rkeithhill GCI Folderthatdoesntexist is asking for the contents of a folder that doesnt exist so that should give some output Get-Printer Doesntexist Should not error IMHO |
Beta Was this translation helpful? Give feedback.
-
This command can be easily made to behave the way you want:
BTW I think you guys are tilting at windmills here. On my Windows 10 1803 system in an elevated Windows PowerShell (more commands built-in), I count the following number of
Of those 41, 29 of them error when the specified item is not found:
That's 71% of the built-in Update: the above results are highly dependent on what modules you have loaded. And it also only checks Get commands with a
And got 41/43 errored which is 95% that error. I'm sure the % will vary depending on the modules you have loaded. |
Beta Was this translation helpful? Give feedback.
-
I know that it can. But I am not just speaking for me but for the people who read all the blog posts where this isn't mentioned for all of the commands they use who are confused or have extra work to do when there is an error. Lets make it easier for people From the original post
If it was green field this would be my view. In the beginning, it was a different view and now the choice has been suggested to have a different view also. I am ok with that, if that is the way the majority want it. I am not doubting all the years of experience that PowerShell committee have but I would say that there are folk commenting here and elsewhere with plenty of years experience as well and their opinion is welcomed as well Also - can I mention my Warning suggestion again ? ;-) |
Beta Was this translation helpful? Give feedback.
-
This is the same deal with why we tell people to never, ever use ambiguous parameter names. What if
|
Beta Was this translation helpful? Give feedback.
-
Fair point. I suppose this would be less than satisfying: |
Beta Was this translation helpful? Give feedback.
-
My expectation and joy of powershell is that Powershell will only error when something is a problem. Powershell has a precedence of great use of errors that help lead scripters to an actionable solution. Not being able to find something does not provide an actionable solution. Null data is not an issue it gives you just as much information about the output as actual data. Embrace null data. |
Beta Was this translation helpful? Give feedback.
-
Hi all. I am new PS user, with not much of experience of writing PS code. It seems more natural to me when you go to a store looking for a thing and it's not there (because it's not there, or you have wrong name of the thing). The result would be : not found, rather than error. Sometimes lack of any message can be confusing as may indicate some other problem. When we apply analogy to search/not-found there is usually something like: But in many cases it would be Information or Warning, not an Error. Initially I might want to see the message, rather then nothing, but it might change when I have more experience with PS. That's from the user perspective. Hope you find it helpful. |
Beta Was this translation helpful? Give feedback.
-
Because more cmdlets don't follow the (good, IMO) example of Get-Command, this has become my scripting pattern:
Every comment line requires a different set of actions. Don't you think it's a bit ridiculous? |
Beta Was this translation helpful? Give feedback.
-
I tend to call almost every command with either Any time I call a cmdlet that gets something, I have to check to make sure I got a value before continuing anyway. I kind of prefer to not return values over creating errors. I like to see errors as something unexpected. I expect that a get command will either get me something or it will not. If we are talking green field here, I would require a I am OK with |
Beta Was this translation helpful? Give feedback.
-
I agree with @KevinMarquette on this one. I also tend to call most commands with But there is the case in point, expected outcome. Errors belong where unexpected outcomes exist, you can test for the expected and either handle or ignore the unexpected.
Another alternative is Or |
Beta Was this translation helpful? Give feedback.
-
Random collection of thoughts:
|
Beta Was this translation helpful? Give feedback.
-
Joey, Re you're being "open to hearing about more scenarios where non-terminating errors are...painful...." At my level, much of my code is within try/catch blocks with ErrorActionPreference set to Stop, so non-terminating errors don't exist. Adding complexity to the error handling to handle "expected" errors different from actual errors is painful. It is much easier to distinguish between an error and a null result if one of them is an error and the other is a null result, instead of them being two different types of errors, terminating or not. And at the level of the junior scripters that make up the bulks of the community, any unnecessary complexity is painful, slows adoption, and steepens the learning curve. It should be possible to add complexity to do complex things, but default behavior should make it simple to do simple things. |
Beta Was this translation helpful? Give feedback.
-
@joeyaiello non-terminating errors are one of the most painful things in PowerShell, because literally at any time a caller of my code may change the rules by just calling my function with |
Beta Was this translation helpful? Give feedback.
-
There's been some discussion recently about when it's appropriate to write errors ...
Get-TypeData
Get-Module
On behalf of the PowerShell Committee, @SteveL-MSFT wrote this:
And then later clarified:
So I ask this community: is that right?
Should a
Get
command that takes a literal name always write an error when it does not return anything?Pretend that PowerShell was green-field, and that whatever you decide here will not break existing code.
Would you want errors from commands like:
What about listing commands where we expect to always have data, but there are edge cases:
Beta Was this translation helpful? Give feedback.
All reactions