google / java-photoslibrary

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

Exception passing in video uploading API #31

Closed zhang123cnn closed 2 years ago

zhang123cnn commented 4 years ago

Hey there,

In code like below, google API is only throwing a generic exception with a generic ENUM. This will not include information in the error response itself.

    HttpResponse response = httpClient.execute(httpPost);

    if (response.getFirstHeader(UPLOAD_GRANULARITY_HEADER) != null) {
      updateOptimalChunkSize(
          Integer.parseInt(response.getFirstHeader(UPLOAD_GRANULARITY_HEADER).getValue()));
    }

    switch (response.getFirstHeader(UPLOAD_STATUS_HEADER).getValue()) {
      case UploadStatuses.ACTIVE:
        return response.getFirstHeader(UPLOAD_URL_HEADER).getValue();
      case UploadStatuses.FINAL:
        throw new IllegalArgumentException(ExceptionStrings.UPLOAD_URL_REJECTED);
      default:
        throw new IllegalStateException(ExceptionStrings.INVALID_UPLOAD_STATUS);
    }

However, as API caller, we might want to see further information associated with the exception to better handle the error case.

For example, when a user uploads a video to an account which does not enable video, then a 403 response would be returned. In this case, it would be very helpful to know it is an 403 error so we can display more relevant guide message to help user resolve the issue. But at the moment, we can't do anything since this information is not popped up to the API caller.

So could we change the code to attach original response error to the exception?

cc @wmorland