Skip to content

Commit 1c577fe

Browse files
some APIs treat 412s as the conflict code. (#4277) (#2753)
* some APIs treat 412s as the conflict code. it stands for 'invalid etag' in that context. * add unit test. Signed-off-by: Modular Magician <[email protected]>
1 parent be1012a commit 1c577fe

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.changelog/4277.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
iam: fixed iam conflict handling so that optimistic-locking retries will succeed more often.
3+
```

google-beta/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ func isFailedPreconditionError(err error) bool {
139139
}
140140

141141
func isConflictError(err error) bool {
142-
if e, ok := err.(*googleapi.Error); ok && e.Code == 409 {
142+
if e, ok := err.(*googleapi.Error); ok && (e.Code == 409 || e.Code == 412) {
143143
return true
144144
} else if !ok && errwrap.ContainsType(err, &googleapi.Error{}) {
145145
e := errwrap.GetType(err, &googleapi.Error{}).(*googleapi.Error)
146-
if e.Code == 409 {
146+
if e.Code == 409 || e.Code == 412 {
147147
return true
148148
}
149149
}

google-beta/utils_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,25 @@ func TestRetryTimeDuration_URLTimeoutsShouldRetry(t *testing.T) {
575575
t.Errorf("expected the retryFunc to be called %v time(s), instead was called %v time(s)", expectedRunCount, runCount)
576576
}
577577
}
578+
579+
func TestConflictError(t *testing.T) {
580+
confErr := &googleapi.Error{
581+
Code: 409,
582+
}
583+
if !isConflictError(confErr) {
584+
t.Error("did not find that a 409 was a conflict error.")
585+
}
586+
if !isConflictError(errwrap.Wrapf("wrap", confErr)) {
587+
t.Error("did not find that a wrapped 409 was a conflict error.")
588+
}
589+
confErr = &googleapi.Error{
590+
Code: 412,
591+
}
592+
if !isConflictError(confErr) {
593+
t.Error("did not find that a 412 was a conflict error.")
594+
}
595+
if !isConflictError(errwrap.Wrapf("wrap", confErr)) {
596+
t.Error("did not find that a wrapped 412 was a conflict error.")
597+
}
598+
// skipping negative tests as other cases may be added later.
599+
}

0 commit comments

Comments
 (0)