nmrshll / google-photos-api-client-go

Google photos api client in go
35 stars 12 forks source link

Add rate limit error handling. #2

Closed nhorvath closed 5 years ago

nhorvath commented 5 years ago

Gracefully handles rate limiting in the Photos API. https://github.com/nmrshll/gphotos-uploader-cli/issues/22

nhorvath commented 5 years ago

@pacoorozco I got this for the first time after making these changes. Could it be caused by anything I did in the last commit?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x70cd9d]

goroutine 12 [running]:
github.com/nmrshll/google-photos-api-client-go/lib-gphotos.(*Client).UploadFile(0xc0001d20e8, 0xc000560000, 0xa3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/nick/go/src/github.com/nmrshll/google-photos-api-client-go/lib-gphotos/client.go:122 +0x3ed
github.com/nmrshll/gphotos-uploader-cli/upload.(*FileUpload).upload(0xc0001d20c0, 0xc000405fa8, 0x1)
        /home/nick/go/src/github.com/nmrshll/gphotos-uploader-cli/upload/fileUpload.go:51 +0x9e
github.com/nmrshll/gphotos-uploader-cli/upload.StartFileUploadWorker.func1(0xc00014a048)
        /home/nick/go/src/github.com/nmrshll/gphotos-uploader-cli/upload/fileUpload.go:31 +0x75
created by github.com/nmrshll/gphotos-uploader-cli/upload.StartFileUploadWorker
        /home/nick/go/src/github.com/nmrshll/gphotos-uploader-cli/upload/fileUpload.go:29 +0x87
exit status 2

edit: yes it's this line

            if batchResponse.ServerResponse.HTTPStatusCode == 429 {
nhorvath commented 5 years ago

@pacoorozco Ah I remember now. You don't have access to the response if error is != nil. That's why I did it this way. From google's docs (emphasis mine): Do executes the "photoslibrary.mediaItems.batchCreate" call. Exactly one of BatchCreateMediaItemsResponse or error will be non-nil. Any non-2xx status code is an error. Response headers are in either BatchCreateMediaItemsResponse.ServerResponse.Header or (if a response was returned at all) in error.(*googleapi.Error).Header. Use googleapi.IsNotModified to check whether the returned error was because http.StatusNotModified was returned.

pacoorozco commented 5 years ago

Yes @nhorvath, you are absolutely right! I don't think Google's doing things easy to deal with errors, but your new code it's right.... Sorry for bother you :-D +1

nmrshll commented 5 years ago

Looks good to merge to me !