Specifically check for timeout error

If you are specifically looking for i/o timeouts, you can use errors.Is to detect an os.ErrDeadlineExceeded error as documented in the net package:

// If the deadline is exceeded a call to Read or Write or to other
// I/O methods will return an error that wraps os.ErrDeadlineExceeded.
// This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
// The error's Timeout method will return true, but note that there
// are other possible errors for which the Timeout method will
// return true even if the deadline has not been exceeded.

if errors.Is(err, os.ErrDeadlineExceeded) {

All possible timeouts will still conform to the net.Error with Timeout() set properly. All you need to check for is:

if err, ok := err.(net.Error); ok && err.Timeout() {

Leave a Comment