I write a simple try catch mechanism in go (here : https://github.com/fzerorubigd/block). 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 )
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
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…