Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling connection reset errors in Go

On a plain Go HTTP handler, if I disconnect a client while still writting to the response, http.ResponseWritter.Write will return an error with a message like write tcp 127.0.0.1:60702: connection reset by peer.

Now from the syscall package, I have sysca.ECONNRESET, which has the message connection reset by peer, so they're not exactly the same.

How can I match them, so I know not to panic if it occurs ? On other ocasions I have been doing

if err == syscall.EAGAIN {
  /* handle error differently */
}

for instance, and that worked fine, but I can't do it with syscall.ECONNRESET.

Update:

Because I'm desperate for a solution, for the time being I'll be doing this very dirty hack:

if strings.Contains(err.Error(), syscall.ECONNRESET.Error()) {
    println("it's a connection reset by peer!")
    return
}
like image 831
João Pinto Jerónimo Avatar asked Nov 12 '13 12:11

João Pinto Jerónimo


1 Answers

The error you get has the underlying type *net.OpError, built here, for example.

You should be able to type-assert the error to its concrete type like this:

operr, ok := err.(*net.OpError)

And then access its Err field, which should correspond to the syscall error you need:

operr.Err.Error() == syscall.ECONNRESET.Error()
like image 152
thwd Avatar answered Oct 03 '22 17:10

thwd