Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acceptable Golang idiomatic nested error handling?

Tags:

idioms

go

I have recently gotten into Go and have seen a lot of discussion about how to do error handling.

the pattern that I have seen laid out is the following:

err := DoSomething()
if err != nil {
   //handle
}
// continue

often times when managing amqp connections, my condition is that I want to continue only if the error is nil, because then I need to do something on the connection:

c, err := Connect()
if err != nil {
   return nil, err
}
s,err := c.RegisterSomethingOnConnection()
if err != nil {
   return nil, err
}
val, err := s.DoSomething()
return val, err

as you can see I only want to run the line c.RegisterSomethingOnConnection if the error returned from Connect() is nil.

However, I dislike the above due to the early returns. Early returns make me uncomfortable because in the long run it hurts readability and obscures exactly when a function exits. My solution so far has been to do the following:

var err error
var val ReturnType

c,err := Connect()
if err == nil {
    s,err := c.RegisterSomethingOnConnection()
    if err == nil {
       val,err = s.DoSomething()
    }
}
return val,err

I like to do this for 2 reasons. First, it prevents returning nil. Second, I find it makes the code more maintainable as you can add easily add functionality before returning (i.e. logging) and not have certain paths miss the added functionality due to an early return.

Is what I have done acceptable idiomatic Go or do I just need to get over my dislike of early returns and follow the that pattern?

like image 621
Fuzzerker Avatar asked Sep 22 '16 13:09

Fuzzerker


People also ask

How are errors handled in Golang?

The idiomatic way of handling errors in Go is to compare the returned error to nil . A nil value indicates that no error has occurred and a non-nil value indicates the presence of an error. In our case, we check whether the error is not nil in line no. 10.

Does Go exceptions Go handle errors?

Go does not have exceptions like many other programming languages, including Java and Javascript but has a comparable mechanism know as ,,Defer, panic and recover".

Does Golang have try catch?

The Go language does not support this method of handling Errors and in this article, we will explore why Golang does not use try-catch for handling errors and how we can use the alternative of this. Before doing so, let's see the syntax of using try-catch in the below section: try{ // Something Executes Here... }

What is the type of err in Golang?

error is a built-in interface type in Go. An error variable represents any value that can describe itself as a string . The interface consists of a single function, Error() , that returns a string error message.


1 Answers

One of the Go Prover is:

Don’t just check errors, handle them gracefully

I recommend you to read this post from Dave Cheney

I put the highlights here:

"There is no single way to handle errors. Instead, I believe Go’s error handling can be classified into the three core strategies"

  • Sentinel errors:

if err == ErrSomething { … }

"Using sentinel values is the least flexible error handling strategy, as the caller must compare the result to predeclared value using the equality operator. This presents a problem when you want to provide more context, as returning a different error would will break the equality check."

  • Error types

if err, ok := err.(SomeType); ok { … }

"An error type is a type that you create that implements the error interface. "

  • Opaque errors

x, err := bar.Foo() if err != nil { return err } // use x

"I call this style opaque error handling, because while you know an error occurred, you don’t have the ability to see inside the error. As the caller, all you know about the result of the operation is that it worked, or it didn’t."

.... read all the post.

I think the important aspect of error handling is to Don’t just check errors, handle them gracefully, I hope this can help you.

like image 61
Eddy Hernandez Avatar answered Sep 18 '22 05:09

Eddy Hernandez