Skip to content

Commit

Permalink
Give proper timeout error message
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Apr 9, 2016
1 parent 0e1057e commit 28c5b9b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (self *Drive) downloadBinary(f *drive.File, args DownloadArgs) (int64, int6

res, err := self.service.Files.Get(f.Id).Context(ctx).Download()
if err != nil {
if isTimeoutError(err) {
return 0, 0, fmt.Errorf("Failed to download file: timeout, no data was transferred for %v", args.Timeout)
}
return 0, 0, fmt.Errorf("Failed to download file: %s", err)
}

Expand Down
5 changes: 5 additions & 0 deletions drive/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package drive

import (
"golang.org/x/net/context"
"google.golang.org/api/googleapi"
"time"
)
Expand All @@ -16,6 +17,10 @@ func isBackendError(err error) bool {
return ok && ae.Code >= 500 && ae.Code <= 599
}

func isTimeoutError(err error) bool {
return err == context.Canceled
}

func exponentialBackoffSleep(try int) {
seconds := pow(2, try)
time.Sleep(time.Duration(seconds) * time.Second)
Expand Down
3 changes: 3 additions & 0 deletions drive/revision_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (self *Drive) DownloadRevision(args DownloadRevisionArgs) (err error) {

res, err := getRev.Context(ctx).Download()
if err != nil {
if isTimeoutError(err) {
return fmt.Errorf("Failed to download file: timeout, no data was transferred for %v", args.Timeout)
}
return fmt.Errorf("Failed to download file: %s", err)
}

Expand Down
2 changes: 2 additions & 0 deletions drive/sync_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func (self *Drive) downloadRemoteFile(id, fpath string, args DownloadSyncArgs, t
exponentialBackoffSleep(try)
try++
return self.downloadRemoteFile(id, fpath, args, try)
} else if isTimeoutError(err) {
return fmt.Errorf("Failed to download file: timeout, no data was transferred for %v", args.Timeout)
} else {
return fmt.Errorf("Failed to download file: %s", err)
}
Expand Down
4 changes: 4 additions & 0 deletions drive/sync_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ func (self *Drive) uploadMissingFile(parentId string, lf *LocalFile, args Upload
exponentialBackoffSleep(try)
try++
return self.uploadMissingFile(parentId, lf, args, try)
} else if isTimeoutError(err) {
return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
} else {
return fmt.Errorf("Failed to upload file: %s", err)
}
Expand Down Expand Up @@ -356,6 +358,8 @@ func (self *Drive) updateChangedFile(cf *changedFile, args UploadSyncArgs, try i
exponentialBackoffSleep(try)
try++
return self.updateChangedFile(cf, args, try)
} else if isTimeoutError(err) {
return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
} else {
return fmt.Errorf("Failed to update file: %s", err)
}
Expand Down
3 changes: 3 additions & 0 deletions drive/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (self *Drive) Update(args UpdateArgs) error {

f, err := self.service.Files.Update(args.Id, dstFile).Fields("id", "name", "size").Context(ctx).Media(reader, chunkSize).Do()
if err != nil {
if isTimeoutError(err) {
return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
}
return fmt.Errorf("Failed to upload file: %s", err)
}

Expand Down
6 changes: 6 additions & 0 deletions drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ func (self *Drive) uploadFile(args UploadArgs) (*drive.File, int64, error) {

f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "md5Checksum", "webContentLink").Context(ctx).Media(reader, chunkSize).Do()
if err != nil {
if isTimeoutError(err) {
return nil, 0, fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
}
return nil, 0, fmt.Errorf("Failed to upload file: %s", err)
}

Expand Down Expand Up @@ -232,6 +235,9 @@ func (self *Drive) UploadStream(args UploadStreamArgs) error {

f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "webContentLink").Context(ctx).Media(reader, chunkSize).Do()
if err != nil {
if isTimeoutError(err) {
return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
}
return fmt.Errorf("Failed to upload file: %s", err)
}

Expand Down

0 comments on commit 28c5b9b

Please sign in to comment.