diff --git a/errors/error.go b/errors/error.go index bb14c1decab71c5f2429035d5d7899ee145815af..d20acd5f24980a0574ec6e6ae2377b32f11a3c26 100644 --- a/errors/error.go +++ b/errors/error.go @@ -9,6 +9,7 @@ import ( ) // CustomError implements the following interfaces: +// // error // github.com/pkg/errors: Cause type CustomError struct { @@ -251,7 +252,7 @@ func IsRetryableError(err error) bool { if code == 0 { return false } - + // 429 should always retry if code == http.StatusTooManyRequests { return true @@ -264,3 +265,34 @@ func IsRetryableError(err error) bool { return false } + +func IsRetryableErrorOrShouldFail(err error) (shouldRetry bool, shouldFail bool) { + if err == nil { + return false, false + } + + // Check for retryable + if IsRetryableError(err) { + return true, false + } + + code := HTTPCode(err) + + // If no HTTP code in the error, just fail but don't retry + if code == 0 { + return false, true + } + + // Explicitly check for 400s, and don't retry or fail + if code >= 400 && code < 500 { + return false, false + } + + // Explicitly check for 200s, for a fail-safe + if code >= 200 && code < 300 { + return false, false + } + + // If we got here, we should fail normally + return false, true +} diff --git a/errors/errors.go b/errors/errors.go index 3c6ab5e25f15f6b7b2c539e584184dbe52efb156..bd6c2afebeb32fa6e72e7c8ef74685757ca87d92 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -102,9 +102,15 @@ func HTTPCodeOnly(code int) error { } func HTTPWithError(code int, err error) error { + var errorMessage string + // This check is here just as a failsafe to seg faults, if err is nil then just return assign an empty string as message. + if err != nil { + errorMessage = err.Error() + } + wrappedErr := &CustomError{ code: code, - message: err.Error(), + message: errorMessage, caller: GetCaller(2), cause: err, }