checking for specific error ?

283 views
Skip to first unread message

vlya...@gmail.com

unread,
Mar 28, 2015, 11:21:19 AM3/28/15
to golan...@googlegroups.com
Is there effective way to check for specific error condition.
For example, tcp client in "C" returns different error codes for different types of failure, such as
ENETUNREACH
Network is unreachable.
ETIMEDOUT
Timeout while attempting connection
EISCONN
The socket is already connected

What is standard method to check for specific error in Go? For example, for "the socket is already connected" with "Dial()" ? 
Thank you

Matt Harden

unread,
Mar 28, 2015, 3:34:56 PM3/28/15
to vlya...@gmail.com, golan...@googlegroups.com
I don't think Dial() could ever give you "the socket is already connected" because it creates a new socket internally before connecting. In general, Go libraries will not always return errors that can be classified like the ones defined by POSIX. Partly this is because different platforms will return different errors. However, the net package does always return errors that implement the net.Error interface, which provides the Timeout() and Temporary() functions for testing the kind of error. What are you specifically trying to achieve?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Victor L

unread,
Mar 30, 2015, 12:38:39 AM3/30/15
to Matt Harden, golan...@googlegroups.com
Thanks for the response, only used "connect" as an example. I am looking for "Go" syntax for checking of the specific error, like "if ( connect() == EISCONN)" ...

Oleg Lomaka

unread,
Mar 30, 2015, 1:43:39 AM3/30/15
to golan...@googlegroups.com, vlya...@gmail.com
Here is an example how to check "Protocol not available"

General error handling may look something like this:

v, err := do_something()
switch {
case err == Error1:
    process_error_1()
case err == Error2:
    process_error_2()
case err != nil:
    process_unexpected_error()
default:
    return do_something_useful_with(v)

Ian Lance Taylor

unread,
Mar 30, 2015, 12:32:37 PM3/30/15
to Victor L, Matt Harden, golan...@googlegroups.com
On Sun, Mar 29, 2015 at 9:38 PM, Victor L <vlya...@gmail.com> wrote:
>
> Thanks for the response, only used "connect" as an example. I am looking for
> "Go" syntax for checking of the specific error, like "if ( connect() ==
> EISCONN)" ...

It's a simple question that doesn't have a simple answer. The Go net
package is designed be OS-independent, so it doesn't directly expose
errno values that are system dependent. It also tries to give more
informative errors, which again tends to hide the errno value.

In general the error returned by a function like Dial will be an
instance of *net.OpError (http://golang.org/pkg/net/#OpError). You
can dig into that to get the real errno value.

For example:

http://play.golang.org/p/EiPiNKMC0R

Ian
Reply all
Reply to author
Forward
0 new messages