google / java-photoslibrary

Java client library for the Google Photos Library API
http://developers.google.com/photos
Apache License 2.0
108 stars 59 forks source link

UploadMediaItem fails with cryptic error when the file length is zero #39

Closed ylexus closed 3 years ago

ylexus commented 3 years ago

When a file length is zero, uploading it causes this exception:

java.lang.IllegalArgumentException: The upload was completed but failed to finalize or get the result.
    at com.google.photos.library.v1.upload.PhotosLibraryUploadCallable.buildUploadMediaItemResponse(PhotosLibraryUploadCallable.java:314) ~[google-photos-library-client-1.5.0.jar:1.5.0]
    at com.google.photos.library.v1.upload.PhotosLibraryUploadCallable.call(PhotosLibraryUploadCallable.java:184) ~[google-photos-library-client-1.5.0.jar:1.5.0]
    at com.google.photos.library.v1.upload.PhotosLibraryUploadCallable.call(PhotosLibraryUploadCallable.java:45) ~[google-photos-library-client-1.5.0.jar:1.5.0]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:?]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:?]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[?:?]
    at java.lang.Thread.run(Unknown Source) ~[?:?]

I think PhotosLibraryUploadCallable should handle it better and fail fast with a more descriptive error.

jfschmakeit commented 3 years ago

The error is actually descriptive - the file was technically uploaded, but the Library API did not return a usable result (ie. an uploadToken). This could have different reasons , including an invalid file like in this case. Here the client library is mirroring the behaviour of the Library API itself and is passing through the response.

However - I agree that it would be nicer to fail early, especially for something that would never succeed. I will propose a change to add a check for the file size before an upload request is made. Failing with a IOException in PhotosLibraryUploadCallable#call()looks like the best option.