Skip to content

Commit bbb2c9c

Browse files
author
ylexus
committed
#123 treat HttpResponseException with code 413 as fatal user correctable error
1 parent f1f1a8f commit bbb2c9c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

app/src/main/java/net/yudichev/googlephotosupload/core/FatalUserCorrectableRemoteApiExceptionHandlerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.ImmutableList;
44
import io.grpc.Status;
55
import io.grpc.StatusRuntimeException;
6+
import org.apache.http.client.HttpResponseException;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
89

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

2829
// https://github.com/ylexus/jiotty-photos-uploader/issues/14: this covers issues like "No permissions to add this media item to the album"
29-
e -> e instanceof StatusRuntimeException && ((StatusRuntimeException) e).getStatus().getCode() == Status.Code.INVALID_ARGUMENT ?
30-
Optional.of(humanReadableMessage(e)) : Optional.empty());
30+
e -> e instanceof StatusRuntimeException sre && sre.getStatus().getCode() == Status.Code.INVALID_ARGUMENT ?
31+
Optional.of(humanReadableMessage(sre)) : Optional.empty(),
32+
33+
// https://github.com/ylexus/jiotty-photos-uploader/issues/123
34+
e -> e instanceof HttpResponseException httpe && httpe.getStatusCode() == 413 ?
35+
Optional.of(httpe.getReasonPhrase()) : Optional.empty());
3136
}
3237

3338
@Override

app/src/test/java/net/yudichev/googlephotosupload/core/FatalUserCorrectableRemoteApiExceptionHandlerImplTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import com.google.api.gax.grpc.GrpcStatusCode;
44
import com.google.api.gax.rpc.ApiException;
55
import io.grpc.Status;
6+
import org.apache.http.client.HttpResponseException;
67
import org.junit.jupiter.api.BeforeEach;
7-
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
811

912
import java.io.IOException;
1013
import java.util.PropertyResourceBundle;
1114
import java.util.ResourceBundle;
15+
import java.util.stream.Stream;
1216

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

29-
@Test
30-
void failedToGetResult() {
31-
var invalidMediaItem = resultHandler.handle("operationName", new ApiException(
32-
new IllegalArgumentException("The file is empty"),
33-
GrpcStatusCode.of(Status.Code.INVALID_ARGUMENT),
34-
true));
35-
assertThat(invalidMediaItem, optionalWithValue(equalTo("oops")));
33+
@ParameterizedTest
34+
@MethodSource
35+
void failedToGetResult(Throwable exception, String expectedErrorMsg) {
36+
var invalidMediaItem = resultHandler.handle("operationName", exception);
37+
assertThat(invalidMediaItem, optionalWithValue(equalTo(expectedErrorMsg)));
38+
}
39+
40+
public static Stream<Arguments> failedToGetResult() {
41+
return Stream.of(
42+
Arguments.of(new ApiException(new IllegalArgumentException("The file is empty"), GrpcStatusCode.of(Status.Code.INVALID_ARGUMENT), true),
43+
"oops"),
44+
Arguments.of(new HttpResponseException(413, "The upload progress could not be verified. Request Entity Too Large"),
45+
"The upload progress could not be verified. Request Entity Too Large")
46+
);
3647
}
3748
}

0 commit comments

Comments
 (0)