| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package utils | 5 package utils |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 » "github.com/luci/luci-go/common/errors" | 8 » "github.com/luci/luci-go/common/retry" |
| 9 "google.golang.org/api/googleapi" | 9 "google.golang.org/api/googleapi" |
| 10 ) | 10 ) |
| 11 | 11 |
| 12 // IsTransientAPIError returns true if error from Google API client indicates | 12 // IsTransientAPIError returns true if error from Google API client indicates |
| 13 // an error that can go away on its own in the future. | 13 // an error that can go away on its own in the future. |
| 14 func IsTransientAPIError(err error) bool { | 14 func IsTransientAPIError(err error) bool { |
| 15 if err == nil { | 15 if err == nil { |
| 16 return false | 16 return false |
| 17 } | 17 } |
| 18 apiErr, _ := err.(*googleapi.Error) | 18 apiErr, _ := err.(*googleapi.Error) |
| 19 if apiErr == nil { | 19 if apiErr == nil { |
| 20 return true // failed to get HTTP code => connectivity error =>
transient | 20 return true // failed to get HTTP code => connectivity error =>
transient |
| 21 } | 21 } |
| 22 return apiErr.Code >= 500 || apiErr.Code == 429 || apiErr.Code == 0 | 22 return apiErr.Code >= 500 || apiErr.Code == 429 || apiErr.Code == 0 |
| 23 } | 23 } |
| 24 | 24 |
| 25 // WrapAPIError wraps error from Google API client in transient wrapper if | 25 // WrapAPIError wraps error from Google API client in transient wrapper if |
| 26 // necessary. | 26 // necessary. |
| 27 func WrapAPIError(err error) error { | 27 func WrapAPIError(err error) error { |
| 28 if IsTransientAPIError(err) { | 28 if IsTransientAPIError(err) { |
| 29 » » return errors.WrapTransient(err) | 29 » » return retry.Tag.Apply(err) |
| 30 } | 30 } |
| 31 return err | 31 return err |
| 32 } | 32 } |
| OLD | NEW |