2015-10-14 16:37:53 +00:00
|
|
|
// Errors and error handling
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-29 15:42:08 +00:00
|
|
|
"io"
|
2015-10-14 16:37:53 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-04-29 15:42:08 +00:00
|
|
|
"strings"
|
2016-06-10 12:48:41 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-10-14 16:37:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Retry is an optional interface for error as to whether the
|
|
|
|
// operation should be retried at a high level.
|
|
|
|
//
|
|
|
|
// This should be returned from Update or Put methods as required
|
|
|
|
type Retry interface {
|
|
|
|
error
|
|
|
|
Retry() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryError is a type of error
|
|
|
|
type retryError string
|
|
|
|
|
|
|
|
// Error interface
|
|
|
|
func (r retryError) Error() string {
|
|
|
|
return string(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry interface
|
|
|
|
func (r retryError) Retry() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface
|
|
|
|
var _ Retry = retryError("")
|
|
|
|
|
|
|
|
// RetryErrorf makes an error which indicates it would like to be retried
|
|
|
|
func RetryErrorf(format string, a ...interface{}) error {
|
|
|
|
return retryError(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlainRetryError is an error wrapped so it will retry
|
|
|
|
type plainRetryError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry interface
|
|
|
|
func (err plainRetryError) Retry() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface
|
|
|
|
var _ Retry = plainRetryError{(error)(nil)}
|
|
|
|
|
|
|
|
// RetryError makes an error which indicates it would like to be retried
|
|
|
|
func RetryError(err error) error {
|
|
|
|
return plainRetryError{err}
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:11:19 +00:00
|
|
|
// IsRetryError returns true if err conforms to the Retry interface
|
|
|
|
// and calling the Retry method returns true.
|
|
|
|
func IsRetryError(err error) bool {
|
|
|
|
if r, ok := err.(Retry); ok {
|
|
|
|
return r.Retry()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-29 15:42:08 +00:00
|
|
|
// isClosedConnError reports whether err is an error from use of a closed
|
|
|
|
// network connection.
|
|
|
|
//
|
|
|
|
// Code adapted from net/http
|
|
|
|
func isClosedConnError(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this error isn't exported so we have to do a
|
|
|
|
// string comparison :-(
|
|
|
|
str := err.Error()
|
|
|
|
if strings.Contains(str, "use of closed network connection") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return isClosedConnErrorPlatform(err)
|
|
|
|
}
|
|
|
|
|
2015-10-14 16:37:53 +00:00
|
|
|
// ShouldRetry looks at an error and tries to work out if retrying the
|
|
|
|
// operation that caused it would be a good idea. It returns true if
|
2016-04-29 15:42:08 +00:00
|
|
|
// the error implements Timeout() or Temporary() or if the error
|
|
|
|
// indicates a premature closing of the connection.
|
2015-10-14 16:37:53 +00:00
|
|
|
func ShouldRetry(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-10 12:48:41 +00:00
|
|
|
// Find root cause if available
|
|
|
|
err = errors.Cause(err)
|
2016-04-29 15:42:08 +00:00
|
|
|
|
2015-10-14 16:37:53 +00:00
|
|
|
// Unwrap url.Error
|
|
|
|
if urlErr, ok := err.(*url.Error); ok {
|
|
|
|
err = urlErr.Err
|
|
|
|
}
|
|
|
|
|
2016-06-10 12:48:41 +00:00
|
|
|
// Look for premature closing of connection
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-10-14 16:37:53 +00:00
|
|
|
// Check for net error Timeout()
|
|
|
|
if x, ok := err.(interface {
|
|
|
|
Timeout() bool
|
|
|
|
}); ok && x.Timeout() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for net error Temporary()
|
|
|
|
if x, ok := err.(interface {
|
|
|
|
Temporary() bool
|
|
|
|
}); ok && x.Temporary() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldRetryHTTP returns a boolean as to whether this resp deserves.
|
|
|
|
// It checks to see if the HTTP response code is in the slice
|
|
|
|
// retryErrorCodes.
|
|
|
|
func ShouldRetryHTTP(resp *http.Response, retryErrorCodes []int) bool {
|
|
|
|
if resp == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, e := range retryErrorCodes {
|
|
|
|
if resp.StatusCode == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|