Skip to content

Commit

Permalink
Only ignore spurious duplicate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowbaq committed Feb 12, 2020
1 parent 5b1692b commit 945b826
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (s *Spreadsheet) DuplicateSheet(title, newTitle string) (*Sheet, error) {
return nil, errors.New("origin sheet does not exist")
}

alreadyExists := s.GetSheet(newTitle)
if alreadyExists != nil {
return nil, errors.New("destination sheet already exist")
}

var maxIndex int64
for _, sheet := range s.Sheets {
if sheet.Properties.Index > maxIndex {
Expand All @@ -79,7 +84,7 @@ func (s *Spreadsheet) DuplicateSheet(title, newTitle string) (*Sheet, error) {
SourceSheetId: origin.Properties.SheetId,
},
})
if err != nil && !isDuplicateSheetError(err) {
if err != nil && !isFakeDuplicateSheetError(err) {
return nil, errors.Wrap(err, "couldn't duplicate sheet")
}

Expand All @@ -91,21 +96,29 @@ func (s *Spreadsheet) DuplicateSheet(title, newTitle string) (*Sheet, error) {
return duplicate, nil
}

func isDuplicateSheetError(err error) bool {
func isFakeDuplicateSheetError(err error) bool {
rerr, ok := err.(retry.Error)
if !ok {
return false
}

for _, e := range rerr.WrappedErrors() {
var (
firstErrorIsNotDuplicate = true
hasSubsequentDuplicate = false
)
for i, e := range rerr.WrappedErrors() {
if gerr, ok := e.(*googleapi.Error); ok {
if gerr.Code == 400 && strings.Contains(gerr.Message, "duplicateSheet") {
return true
if i == 0 {
firstErrorIsNotDuplicate = false
} else {
hasSubsequentDuplicate = true
}
}
}
}

return false
return firstErrorIsNotDuplicate && hasSubsequentDuplicate
}

func (s *Sheet) Title() string {
Expand Down

0 comments on commit 945b826

Please sign in to comment.