go
go copied to clipboard
Implement new Concept Exercise: panic-recover
This issue describes how to implement the panic-recover concept exercise for the Go track.
Getting started
The docs can be found here
Also, be aware of these general guidelines:
Goal
The goal of this exercise is to teach the student how to work with panic and recover in Go.
Learning objectives
panicwith error or other valuerecovera panic- check the value type of the recover value
don't panic: if, then only inmainpackage to crash the program (e.g. if not all required services are reachable)- other things that can panic: nil pointer dereference, index out of bounds, etc.
- A panic in the code is always a bug --> fix it! (don't use it as
try .. catchalternative!) - global panic handling in e.g. web-servers: their reason is to prevent potential bugs from crashing the entire server
Out of scope
Nothing yet.
Concepts
panic-recover
Prerequisites
defertype-assertions
Resources to refer to
- https://blog.golang.org/defer-panic-and-recover
Implementing
Some function(s) that panic and the panic must be recovered in another function and turned into a proper error.
As an idea on how to teach "when not to use a panic/not to handle a panic but instead fix the bug":
Some functions could be already in the stub that panic on running the tests and the students task is to fix the problem. (they should fix the bug, not use recover)
Help
If you have any questions while implementing the exercise, please post the questions as comments in this issue.
Hey guys, Im a goloang developer, and also a learner from you website. I want to contribute to this issue
@myworkcircle It would be great if you could help out with this issue too but maybe it makes sense you work on the smaller one (https://github.com/exercism/go/issues/1989) first, see how you get along and once that PR is done and you developed a feeling for the work that needs to be done, you can work on this issue. Is that ok for you?
sure, fine with me. Thanx a lot
@myworkcircle Haven't heard from you in a while. Are you still interested in working on this issue?
@junedev Really sorry for being inactive in the past couple of weeks, i was really busy with my company's work due to some strict deadlines. I have already started on this issue last week. Im figuring out how to write the test cases for panic recover. Im working on it, will try to complete it as soon as possible
@myworkcircle Great to hear from you. There is no rush, I was mainly asking because I did not assign you to the issue yet. I will do this now.
As for testing the panic part, maybe you can take some inspiration from how testing frameworks like https://github.com/stretchr/testify do this.
Thanks @junedev
@myworkcircle Can you leave a quick note about the status and whether you plan to continue working on this?
@junedev related: https://github.com/exercism/go/pull/2133#issuecomment-1100138495
@junedev In this exercise should i be writing a testCase for Recover() function also? My question is that, i have added a testcase which for a method AddPanic() which let us know if user have written some code which created panic now, if i want to write a testCase that checks whether user has used panic recovery.
My question basically is, how do i write a testCase that checks for panic created by function and then same panic should be recovered in another function function. To do that recover in exampler.go in the AddPanic() method i need to defer a call to RecoverPanic() method, but then the panic creation test case fails.
You can check the PR for more reference as what i have done till now. https://github.com/exercism/go/pull/2133
I have removed AddPanic() method. I have simply written testCases for checking if the user has recovered panick in another function and if the user has resolved the panic condition in another function
@myworkcircle I haven't looked at the current state of the PR, just leaving some thoughts in the issue. I was about to suggest something similar/the same as you mentioned above. There should be a task that requires writing a function that calls a function (provided as argument) that panics. Then the student is supposed to e.g. return the error message that was revealed when recovering the panic. Then your tests can check there was no panic when calling the students function and that the correct message was identified. Here a rough example (naming needs to be adapted to the story of course):
func identifyError(callMe func()) string {
// student should call "callMe" which panics, recover the error and return the error message
}