Skip to content

Commit d6342a6

Browse files
committed
Fix crash in error handling of IOExceptions without messages
1 parent aba221e commit d6342a6

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

gradle/versions.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ext {
2626

2727
junitVersion = '4.12'
2828
assertjVersion = '3.11.0'
29+
mockitoVersion = '2.22.0'
2930
privateConstructorVersion = '1.2.0'
3031

3132
deps = [
@@ -42,6 +43,7 @@ ext {
4243

4344
junit : "junit:junit:$junitVersion",
4445
assertj : "org.assertj:assertj-core:$assertjVersion",
46+
mockito : "org.mockito:mockito-android:$mockitoVersion",
4547
okhttpTls : "com.squareup.okhttp3:okhttp-tls:$okHttpVersion",
4648
mockwebserver : "com.squareup.okhttp3:mockwebserver:$okHttpVersion",
4749
privateConstructor: "com.pushtorefresh.java-private-constructor-checker:checker:$privateConstructorVersion"

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies {
4444

4545
testImplementation deps.junit
4646
testImplementation deps.assertj
47+
testImplementation deps.mockito
4748
testImplementation deps.okhttpTls
4849
testImplementation deps.mockwebserver
4950
testImplementation deps.privateConstructor

library/src/main/java/me/proxer/library/api/ProxerCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private ProxerException processNonProxerError(final Exception error) {
149149
if (error instanceof SocketTimeoutException) {
150150
return new ProxerException(ProxerException.ErrorType.TIMEOUT, error);
151151
} else if (error instanceof IOException) {
152-
if (error.getMessage().equals("Canceled")) {
152+
if (error.getMessage() != null && error.getMessage().equals("Canceled")) {
153153
return new ProxerException(ProxerException.ErrorType.CANCELLED, error);
154154
} else {
155155
return new ProxerException(ProxerException.ErrorType.IO, error);

library/src/test/java/me/proxer/library/api/ProxerCallTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
import okhttp3.mockwebserver.MockResponse;
88
import okhttp3.mockwebserver.SocketPolicy;
99
import org.junit.Test;
10+
import retrofit2.Call;
1011

1112
import java.io.IOException;
1213
import java.util.List;
1314
import java.util.concurrent.CountDownLatch;
1415

1516
import static org.assertj.core.api.Assertions.assertThat;
1617
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
1720

1821
/**
1922
* @author Ruben Gees
@@ -172,4 +175,18 @@ public void testClone() throws ProxerException, IOException {
172175
public void testRequest() {
173176
assertThat(api.notifications().news().build().request()).isNotNull();
174177
}
178+
179+
@SuppressWarnings("unchecked")
180+
@Test
181+
public void testRequestThrowingErrorWithoutMessage() throws IOException {
182+
final Call<ProxerResponse<NewsArticle>> mockedCall = mock(Call.class);
183+
final ProxerCall<NewsArticle> call = new ProxerCall<>(mockedCall);
184+
185+
when(mockedCall.execute()).thenThrow(new IOException());
186+
187+
assertThatExceptionOfType(ProxerException.class)
188+
.isThrownBy(call::execute)
189+
.withCauseExactlyInstanceOf(IOException.class)
190+
.matches(it -> it.getErrorType() == ErrorType.IO);
191+
}
175192
}

0 commit comments

Comments
 (0)