Filter out broken pipe errors

后端 未结 3 1236
挽巷
挽巷 2021-01-11 11:46

I\'m getting an error returned from an io.Copy call, to which I\'ve passed a socket (TCPConn) as the destination. It\'s expected that

相关标签:
3条回答
  • 2021-01-11 11:53

    The broken pipe error is defined in the syscall package. You can use the equality operator to compare the error to the one in syscall. Check http://golang.org/pkg/syscall/#constants for a complete list of syscall errors. Search "EPIPE" on the page and you will find all the defined errors grouped together.

    if err == syscall.EPIPE {
        /* ignore */
    }
    

    If you wish to get the actual errno number (although it is pretty useless) you can use a type assertion:

    if e, ok := err.(syscall.Errno); ok {
        errno = uintptr(e)
    }
    
    0 讨论(0)
  • 2021-01-11 11:59

    Having all but an error interface is enough to perform a type assertion or a type switch to reveal the concrete type held by the interface.

    0 讨论(0)
  • 2021-01-11 12:12

    As of go 1.13, you can use errors.Is instead of type assertions.

    if errors.Is(err, syscall.EPIPE) {
      // broken pipe
    }
    
    0 讨论(0)
提交回复
热议问题