Skip to content

Commit 709dacc

Browse files
macgillsashishkumar468
authored andcommitted
commons-app#3658 Throw exception instead of allowing nullable network models onError - peek response body and throw error (commons-app#3659)
* commons-app#3658 Throw exception instead of allowing nullable network models onError - peek response body and throw error * commons-app#3658 Throw exception instead of allowing nullable network models onError - allow for more general error response catching
1 parent 0677adf commit 709dacc

File tree

1 file changed

+6
-32
lines changed

1 file changed

+6
-32
lines changed

app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java

+6-32
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import androidx.annotation.NonNull;
44
import java.io.File;
55
import java.io.IOException;
6-
import java.util.Arrays;
7-
import java.util.Collections;
8-
import java.util.List;
96
import okhttp3.Cache;
107
import okhttp3.Interceptor;
118
import okhttp3.OkHttpClient;
@@ -24,8 +21,7 @@ public final class OkHttpConnectionFactory {
2421
@NonNull private static final Cache NET_CACHE = new Cache(new File(CommonsApplication.getInstance().getCacheDir(),
2522
CACHE_DIR_NAME), NET_CACHE_SIZE);
2623

27-
@NonNull
28-
private static final OkHttpClient CLIENT = createClient();
24+
@NonNull private static final OkHttpClient CLIENT = createClient();
2925

3026
@NonNull public static OkHttpClient getClient() {
3127
return CLIENT;
@@ -44,7 +40,7 @@ private static OkHttpClient createClient() {
4440

4541
private static HttpLoggingInterceptor getLoggingInterceptor() {
4642
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor()
47-
.setLevel(Level.BASIC);
43+
.setLevel(Level.BASIC);
4844

4945
httpLoggingInterceptor.redactHeader("Authorization");
5046
httpLoggingInterceptor.redactHeader("Cookie");
@@ -53,10 +49,7 @@ private static HttpLoggingInterceptor getLoggingInterceptor() {
5349
}
5450

5551
private static class CommonHeaderRequestInterceptor implements Interceptor {
56-
57-
@Override
58-
@NonNull
59-
public Response intercept(@NonNull final Chain chain) throws IOException {
52+
@Override @NonNull public Response intercept(@NonNull final Chain chain) throws IOException {
6053
final Request request = chain.request().newBuilder()
6154
.header("User-Agent", CommonsApplication.getInstance().getUserAgent())
6255
.build();
@@ -65,44 +58,25 @@ public Response intercept(@NonNull final Chain chain) throws IOException {
6558
}
6659

6760
public static class UnsuccessfulResponseInterceptor implements Interceptor {
68-
private static final List<String> DO_NOT_INTERCEPT = Collections.singletonList(
69-
"api.php?format=json&formatversion=2&errorformat=plaintext&action=upload&ignorewarnings=1");
7061

7162
private static final String ERRORS_PREFIX = "{\"error";
7263

73-
@Override
74-
@NonNull
75-
public Response intercept(@NonNull final Chain chain) throws IOException {
64+
@Override @NonNull public Response intercept(@NonNull final Chain chain) throws IOException {
7665
final Response rsp = chain.proceed(chain.request());
77-
78-
// Do not intercept certain requests and let the caller handle the errors
79-
if(isExcludedUrl(chain.request())) {
80-
return rsp;
81-
}
8266
if (rsp.isSuccessful()) {
8367
try (final ResponseBody responseBody = rsp.peekBody(ERRORS_PREFIX.length())) {
84-
if (ERRORS_PREFIX.equals(responseBody.string())) {
68+
if (ERRORS_PREFIX.equals(responseBody.string())){
8569
try (final ResponseBody body = rsp.body()) {
8670
throw new IOException(body.string());
8771
}
8872
}
89-
} catch (final IOException e) {
73+
}catch (final IOException e){
9074
Timber.e(e);
9175
}
9276
return rsp;
9377
}
9478
throw new HttpStatusException(rsp);
9579
}
96-
97-
private boolean isExcludedUrl(final Request request) {
98-
final String requestUrl = request.url().toString();
99-
for(final String url: DO_NOT_INTERCEPT) {
100-
if(requestUrl.contains(url)) {
101-
return true;
102-
}
103-
}
104-
return false;
105-
}
10680
}
10781

10882
private OkHttpConnectionFactory() {

0 commit comments

Comments
 (0)