go icon indicating copy to clipboard operation
go copied to clipboard

Implement new Concept Exercise: panic-recover

Open tehsphinx opened this issue 5 years ago • 13 comments
trafficstars

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

  • panic with error or other value
  • recover a panic
  • check the value type of the recover value
  • don't panic: if, then only in main package 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 .. catch alternative!)
  • 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

  • defer
  • type-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.

tehsphinx avatar Mar 01 '20 15:03 tehsphinx

Hey guys, Im a goloang developer, and also a learner from you website. I want to contribute to this issue

myworkcircle avatar Jan 30 '22 18:01 myworkcircle

@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?

junedev avatar Jan 30 '22 19:01 junedev

sure, fine with me. Thanx a lot

myworkcircle avatar Jan 31 '22 17:01 myworkcircle

@myworkcircle Haven't heard from you in a while. Are you still interested in working on this issue?

junedev avatar Mar 16 '22 20:03 junedev

@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 avatar Mar 19 '22 09:03 myworkcircle

@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.

junedev avatar Mar 19 '22 12:03 junedev

Thanks @junedev

myworkcircle avatar Mar 19 '22 13:03 myworkcircle

@myworkcircle Can you leave a quick note about the status and whether you plan to continue working on this?

junedev avatar Jun 09 '22 18:06 junedev

@junedev related: https://github.com/exercism/go/pull/2133#issuecomment-1100138495

andrerfcsantos avatar Jun 09 '22 18:06 andrerfcsantos

@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.

myworkcircle avatar Jul 10 '22 07:07 myworkcircle

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

myworkcircle avatar Jul 10 '22 09:07 myworkcircle

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 avatar Jul 10 '22 09:07 myworkcircle

@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
}

junedev avatar Jul 12 '22 13:07 junedev