Block, a simple try/catch like mechanism

I write a simple try catch mechanism in go (here : and No, it’s not about/related to panic.
its just a simple way to handle error value in a chain of functions in order to check error value in more than just a if err != nil {} .
Normally, I use if/else/else if and even switch/case for this task, but today I write a code with 4 if after the call to a function and then every error value need its own handler. (I know, but the api I work with is not a good one :confused: )
currently I use reflection in this library, and I know normal if can handle it too, but in my case, I found this useful.

this post, is not just an announcement, its also a help request, if anyone had a better idea to handle this case, please share :slight_smile:

Thank you.

1 Like

	// handle finally
	switch err.(type) {
	case *os.PathError:
		fmt.Println("PATH ERROR")
	case error:
		fmt.Println("SOME OTHER ERROR")
		fmt.Println("NO ERROR")
1 Like

Not in my case. I am aware of the .(type) but in my case i need to pass one error to multiple handler, and even change the error to the real error in some case (don’t blame me for this mess in API :)) ), I think fallthrough keyword is needed here.

my old way is like this :

err := myApiCal() 
if e, ok := err.(ErrType1); ok {
   // do some log and othe things 
   err = e.InnerError() // inner error is the actual error from another endpoint, or may be another call that must be done in this erro case
// I can use switch err.(type){} here 
if e, ok := err.(OtherType); ok {
   // do more stuff
  err = nil 
//and so on....

You seem to be using an annoying and potentially broken API. Perhaps the right answer is to wrap it (if fixing it is out of the question) and then not have to suffer this pain when using it…

I am responsible for writing this wrap in my team :expressionless: and I need to handle all of this cases in my wrap. Yes, fixing the api is out of my scope :confused:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.