mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
#3658 Throw exception instead of allowing nullable network models onError - peek response body and throw error (#3659)
* #3658 Throw exception instead of allowing nullable network models onError - peek response body and throw error * #3658 Throw exception instead of allowing nullable network models onError - allow for more general error response catching
This commit is contained in:
parent
07f19c470d
commit
ff310e43e2
1 changed files with 24 additions and 12 deletions
|
|
@ -1,19 +1,19 @@
|
||||||
package fr.free.nrw.commons;
|
package fr.free.nrw.commons;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import org.wikipedia.dataclient.SharedPreferenceCookieManager;
|
|
||||||
import org.wikipedia.dataclient.okhttp.HttpStatusException;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
import okhttp3.Interceptor;
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
import okhttp3.logging.HttpLoggingInterceptor;
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor.Level;
|
||||||
|
import org.wikipedia.dataclient.SharedPreferenceCookieManager;
|
||||||
|
import org.wikipedia.dataclient.okhttp.HttpStatusException;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
public final class OkHttpConnectionFactory {
|
public final class OkHttpConnectionFactory {
|
||||||
private static final String CACHE_DIR_NAME = "okhttp-cache";
|
private static final String CACHE_DIR_NAME = "okhttp-cache";
|
||||||
|
|
@ -21,7 +21,7 @@ public final class OkHttpConnectionFactory {
|
||||||
@NonNull private static final Cache NET_CACHE = new Cache(new File(CommonsApplication.getInstance().getCacheDir(),
|
@NonNull private static final Cache NET_CACHE = new Cache(new File(CommonsApplication.getInstance().getCacheDir(),
|
||||||
CACHE_DIR_NAME), NET_CACHE_SIZE);
|
CACHE_DIR_NAME), NET_CACHE_SIZE);
|
||||||
|
|
||||||
@NonNull private static OkHttpClient CLIENT = createClient();
|
@NonNull private static final OkHttpClient CLIENT = createClient();
|
||||||
|
|
||||||
@NonNull public static OkHttpClient getClient() {
|
@NonNull public static OkHttpClient getClient() {
|
||||||
return CLIENT;
|
return CLIENT;
|
||||||
|
|
@ -39,8 +39,8 @@ public final class OkHttpConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpLoggingInterceptor getLoggingInterceptor() {
|
private static HttpLoggingInterceptor getLoggingInterceptor() {
|
||||||
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor()
|
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor()
|
||||||
.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
.setLevel(Level.BASIC);
|
||||||
|
|
||||||
httpLoggingInterceptor.redactHeader("Authorization");
|
httpLoggingInterceptor.redactHeader("Authorization");
|
||||||
httpLoggingInterceptor.redactHeader("Cookie");
|
httpLoggingInterceptor.redactHeader("Cookie");
|
||||||
|
|
@ -49,8 +49,8 @@ public final class OkHttpConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CommonHeaderRequestInterceptor implements Interceptor {
|
private static class CommonHeaderRequestInterceptor implements Interceptor {
|
||||||
@Override @NonNull public Response intercept(@NonNull Chain chain) throws IOException {
|
@Override @NonNull public Response intercept(@NonNull final Chain chain) throws IOException {
|
||||||
Request request = chain.request().newBuilder()
|
final Request request = chain.request().newBuilder()
|
||||||
.header("User-Agent", CommonsApplication.getInstance().getUserAgent())
|
.header("User-Agent", CommonsApplication.getInstance().getUserAgent())
|
||||||
.build();
|
.build();
|
||||||
return chain.proceed(request);
|
return chain.proceed(request);
|
||||||
|
|
@ -58,9 +58,21 @@ public final class OkHttpConnectionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class UnsuccessfulResponseInterceptor implements Interceptor {
|
public static class UnsuccessfulResponseInterceptor implements Interceptor {
|
||||||
@Override @NonNull public Response intercept(@NonNull Chain chain) throws IOException {
|
|
||||||
Response rsp = chain.proceed(chain.request());
|
private static final String ERRORS_PREFIX = "{\"error";
|
||||||
|
|
||||||
|
@Override @NonNull public Response intercept(@NonNull final Chain chain) throws IOException {
|
||||||
|
final Response rsp = chain.proceed(chain.request());
|
||||||
if (rsp.isSuccessful()) {
|
if (rsp.isSuccessful()) {
|
||||||
|
try (final ResponseBody responseBody = rsp.peekBody(ERRORS_PREFIX.length())) {
|
||||||
|
if (ERRORS_PREFIX.equals(responseBody.string())){
|
||||||
|
try (final ResponseBody body = rsp.body()) {
|
||||||
|
throw new IOException(body.string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (final IOException e){
|
||||||
|
Timber.e(e);
|
||||||
|
}
|
||||||
return rsp;
|
return rsp;
|
||||||
}
|
}
|
||||||
throw new HttpStatusException(rsp);
|
throw new HttpStatusException(rsp);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue