Skip to content

Commit

Permalink
#123 treat HttpResponseException with code 413 as fatal user correcta…
Browse files Browse the repository at this point in the history
…ble error
  • Loading branch information
ylexus committed Sep 15, 2022
1 parent f1f1a8f commit bbb2c9c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.apache.http.client.HttpResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -26,8 +27,12 @@ final class FatalUserCorrectableRemoteApiExceptionHandlerImpl implements FatalUs
Optional.of(resourceBundle.getString("fatalUserCorrectableRemoteApiException.maybeEmptyFile")) : Optional.empty(),

// https://github.com/ylexus/jiotty-photos-uploader/issues/14: this covers issues like "No permissions to add this media item to the album"
e -> e instanceof StatusRuntimeException && ((StatusRuntimeException) e).getStatus().getCode() == Status.Code.INVALID_ARGUMENT ?
Optional.of(humanReadableMessage(e)) : Optional.empty());
e -> e instanceof StatusRuntimeException sre && sre.getStatus().getCode() == Status.Code.INVALID_ARGUMENT ?
Optional.of(humanReadableMessage(sre)) : Optional.empty(),

// https://github.com/ylexus/jiotty-photos-uploader/issues/123
e -> e instanceof HttpResponseException httpe && httpe.getStatusCode() == 413 ?
Optional.of(httpe.getReasonPhrase()) : Optional.empty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.ApiException;
import io.grpc.Status;
import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.stream.Stream;

import static net.yudichev.googlephotosupload.core.OptionalMatchers.optionalWithValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -26,12 +30,19 @@ void setUp() throws IOException {
resultHandler = new FatalUserCorrectableRemoteApiExceptionHandlerImpl(resourceBundle);
}

@Test
void failedToGetResult() {
var invalidMediaItem = resultHandler.handle("operationName", new ApiException(
new IllegalArgumentException("The file is empty"),
GrpcStatusCode.of(Status.Code.INVALID_ARGUMENT),
true));
assertThat(invalidMediaItem, optionalWithValue(equalTo("oops")));
@ParameterizedTest
@MethodSource
void failedToGetResult(Throwable exception, String expectedErrorMsg) {
var invalidMediaItem = resultHandler.handle("operationName", exception);
assertThat(invalidMediaItem, optionalWithValue(equalTo(expectedErrorMsg)));
}

public static Stream<Arguments> failedToGetResult() {
return Stream.of(
Arguments.of(new ApiException(new IllegalArgumentException("The file is empty"), GrpcStatusCode.of(Status.Code.INVALID_ARGUMENT), true),
"oops"),
Arguments.of(new HttpResponseException(413, "The upload progress could not be verified. Request Entity Too Large"),
"The upload progress could not be verified. Request Entity Too Large")
);
}
}

0 comments on commit bbb2c9c

Please sign in to comment.