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
Error handling koans #153
Error handling koans #153
Conversation
8253c2a
to
60f63ea
Compare
Error Handling in PowerShell | ||
|
||
PowerShell handles errors via the System.Management.Automation.ErrorRecord class. These objects form | ||
the basic unit of "an error" in PowerShell. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this sentence after coming here to learn about errors I'd be very confused. It is overly technical but also abstract. What does >>basic unit of "an error"<< even mean? I'd rephrase it with a beginner in mind, without trying to squeeze too much ideas into the first sentence they see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, good point. Maybe something more like Each error is represented in PowerShell by a corresponding ErrorRecord object.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK, is that the first thing to learn about errors? I'd start from the terminating, non-terminating categorisation. You are mentioning ErrorRecord before the first task, so I'd just delete this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it would make any sense to talk about errors at all in PowerShell before at least giving an overview of what they are... 😕
PowerShell handles errors via the System.Management.Automation.ErrorRecord class. These objects form | ||
the basic unit of "an error" in PowerShell. | ||
|
||
Errors can be flagged as terminating or non-terminating based on a few different criteria, largely |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagged by who, based on which criteria? :) Be more authoritative, you are the teacher here. It does not matter that you leave out some detail or an edge-case. Give solid facts first. "Errors in PowerShell are terminating and non-terminating. The type of error that a command throws depends on the severity of the error. If the command cannot continue it throws a terminating error, if it can continue it throws a non-terminating error.
example 1: a built-in cmdlet throwing terminating error
example 2: a built-in cmdlet throwing non-terminating error
To handle terminating errors we use try/catch etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authoritative is a bit much I feel, but I know what you mean. I'll see how I can rephrase this 😄
metadata that you can work with if you come across an error. | ||
#> | ||
BeforeAll { | ||
$Record = try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call this ErrorRecord
Note that specifying the exception type is optional. A "generic" catch block (no exception type | ||
specified) will simply catch everything. If you know the errors you're looking for, it's often | ||
best to only catch the ones you're expecting. If something else happens, you might make matters | ||
worse by handling that the same way as the errors you were expecting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A short example of bad error handling practice might be useful here e.g. try { throw [InvalidOperationException]"Cannot write into file log.txt" } catch { Write-Host "Cannot connect to network" }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, thanks!
@nohwnd if you have a moment to check through my latest changes and let me know if there's anything more that needs tinkering with, I'd greatly appreciate it <3 |
1947f95
to
4a4494a
Compare
PR Summary
Add error handling koans!
TODO
catch [Type] {}
try{} finally{}
trap{}
[shudder] exampleAdd(this should be included in Add Topic - AboutCommonParameters / AboutPreferenceVariables #154 instead)-ErrorAction
and$ErrorActionPreference
examples