11package fr .free .nrw .commons ;
22
33import androidx .annotation .NonNull ;
4-
5- import org .wikipedia .dataclient .SharedPreferenceCookieManager ;
6- import org .wikipedia .dataclient .okhttp .HttpStatusException ;
7-
84import java .io .File ;
95import java .io .IOException ;
10-
116import okhttp3 .Cache ;
127import okhttp3 .Interceptor ;
138import okhttp3 .OkHttpClient ;
149import okhttp3 .Request ;
1510import okhttp3 .Response ;
11+ import okhttp3 .ResponseBody ;
1612import okhttp3 .logging .HttpLoggingInterceptor ;
13+ import okhttp3 .logging .HttpLoggingInterceptor .Level ;
14+ import org .wikipedia .dataclient .SharedPreferenceCookieManager ;
15+ import org .wikipedia .dataclient .okhttp .HttpStatusException ;
16+ import timber .log .Timber ;
1717
1818public final class OkHttpConnectionFactory {
1919 private static final String CACHE_DIR_NAME = "okhttp-cache" ;
2020 private static final long NET_CACHE_SIZE = 64 * 1024 * 1024 ;
2121 @ NonNull private static final Cache NET_CACHE = new Cache (new File (CommonsApplication .getInstance ().getCacheDir (),
2222 CACHE_DIR_NAME ), NET_CACHE_SIZE );
2323
24- @ NonNull private static OkHttpClient CLIENT = createClient ();
24+ @ NonNull private static final OkHttpClient CLIENT = createClient ();
2525
2626 @ NonNull public static OkHttpClient getClient () {
2727 return CLIENT ;
@@ -39,8 +39,8 @@ private static OkHttpClient createClient() {
3939 }
4040
4141 private static HttpLoggingInterceptor getLoggingInterceptor () {
42- HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor ()
43- .setLevel (HttpLoggingInterceptor . Level .BASIC );
42+ final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor ()
43+ .setLevel (Level .BASIC );
4444
4545 httpLoggingInterceptor .redactHeader ("Authorization" );
4646 httpLoggingInterceptor .redactHeader ("Cookie" );
@@ -49,18 +49,30 @@ private static HttpLoggingInterceptor getLoggingInterceptor() {
4949 }
5050
5151 private static class CommonHeaderRequestInterceptor implements Interceptor {
52- @ Override @ NonNull public Response intercept (@ NonNull Chain chain ) throws IOException {
53- Request request = chain .request ().newBuilder ()
52+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
53+ final Request request = chain .request ().newBuilder ()
5454 .header ("User-Agent" , CommonsApplication .getInstance ().getUserAgent ())
5555 .build ();
5656 return chain .proceed (request );
5757 }
5858 }
5959
6060 public static class UnsuccessfulResponseInterceptor implements Interceptor {
61- @ Override @ NonNull public Response intercept (@ NonNull Chain chain ) throws IOException {
62- Response rsp = chain .proceed (chain .request ());
61+
62+ private static final String ERRORS_PREFIX = "{\" error" ;
63+
64+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
65+ final Response rsp = chain .proceed (chain .request ());
6366 if (rsp .isSuccessful ()) {
67+ try (final ResponseBody responseBody = rsp .peekBody (ERRORS_PREFIX .length ())) {
68+ if (ERRORS_PREFIX .equals (responseBody .string ())){
69+ try (final ResponseBody body = rsp .body ()) {
70+ throw new IOException (body .string ());
71+ }
72+ }
73+ }catch (final IOException e ){
74+ Timber .e (e );
75+ }
6476 return rsp ;
6577 }
6678 throw new HttpStatusException (rsp );
0 commit comments