Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go error handling, type assertion, and the net package

I'm learning go and trying to understand how to get more detailed error information out of the generic error type. The example I'll use is from the net package, specifically the DialTimeout function.

The signature is

func DialTimeout(network, address string, timeout time.Duration) (Conn, error)

The error type only defines an Error() string function. If I want to find out exactly why DialTimeout failed, how can I get that information? I found out that I can use type assertion to get the net.Error specific error:

con, err := net.DialTimeout("tcp", net.JoinHostPort(address, "22"),
    time.Duration(5) * time.Second)
if err != nil {
    netErr, ok := err.(net.Error)
    if ok && netErr.Timeout() {
        // ...
    } 
} 

but that only tells me whether or not I had a timeout. For example, say I wanted to differentiate between a refused connection and no route to host. How can I do that?

Maybe DialTimeout is too high-level to give me that kind of detail, but even looking at syscall.Connect, I don't see how to get the specific error. It just says it returns the generic error type. Compare that to the Posix connect, which will let me know why it failed with the various return codes.

My general question is: how am I supposed to pull out error details from the generic error type if the golang docs don't tell me what type of errors may be returned?

like image 245
Neal Avatar asked Jan 04 '14 21:01

Neal


People also ask

How do you handle errors in Go?

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.

What is Golang error type?

error is a built-in interface type in Go. An error variable represents any value that can describe itself as a string . The following is the error interface declaration: type error interface { Error() string. }

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

What are the functions present in the errors package?

The errors.Is function compares an error to a value. The As function tests whether an error is a specific type. The errors package also includes a new Unwrap function which returns the result of calling an error's Unwrap method, or nil when the error has no Unwrap method.


1 Answers

Most networking operations return a *OpError which holds detailed information about the error and implements the net.Error interface. So for most use cases it is sufficient to use net.Error as you already did.

But for your case you'd want to assert the returned error to be *net.OpError and use the internal error:

if err != nil {
    if oerr, ok := err.(*OpError); ok {
        // Do something with oerr.Err
    }
}

As soon as you're doing this you are in the land of platform dependency as syscalls under Linux can fail differently to those under Windows. For Linux you'd do something like this:

if oerr.Err == syscall.ECONNREFUSED {
    // Connection was refused :(
}

The syscall package contains the important error constants for your platform. Unfortunately the golang website only shows the syscall package for Linux amd64. See here for ECONNREFUSED.

Finding out types

The next time you're wondering what is actually returned by some function and you can't make heads and tails of it, try using the %#v format specified in fmt.Printf (and friends):

 fmt.Printf("%#v\n", err)
 // &net.OpError{Op:"dial", Net:"tcp", Addr:(*net.TCPAddr)(0xc20006d390), Err:0x6f}

It will print detailed type information and is generally quite helpful.

like image 79
nemo Avatar answered Oct 02 '22 06:10

nemo