From 3e96ac207e69ced1397edadacc986c8f530c0e27 Mon Sep 17 00:00:00 2001 From: Hector Date: Sat, 12 Oct 2019 19:36:55 +0200 Subject: [PATCH] =?UTF-8?q?Created=20new=20LoggingInterceptor=20that=20doe?= =?UTF-8?q?sn't=20log=20any=20errors=20related=20to=E2=80=A6=20(#291)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Created new LoggingInterceptor that doesn't log any errors related to SocketTimeoutException. * Formatted with ktlint * Intercept timeoutException to bypass the logging but still throwing an exception. --- .../api/selfoss/SelfossApi.kt | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index 5327add..56fe70c 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -12,14 +12,12 @@ import com.burgstaller.okhttp.digest.CachingAuthenticator import com.burgstaller.okhttp.digest.Credentials import com.burgstaller.okhttp.digest.DigestAuthenticator import com.google.gson.GsonBuilder -import okhttp3.Interceptor -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response +import okhttp3.* import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Call import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory +import java.net.SocketTimeoutException import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.TimeUnit @@ -97,15 +95,38 @@ class SelfossApi( val logging = HttpLoggingInterceptor() + logging.level = if (shouldLog) { HttpLoggingInterceptor.Level.BODY } else { HttpLoggingInterceptor.Level.NONE } - val httpClient = authenticator.getHttpClien(isWithSelfSignedCert, timeout) - httpClient.addInterceptor(logging) + val timeoutCode = 504 + httpClient + .addInterceptor { chain -> + val res = chain.proceed(chain.request()) + if (res.code() == timeoutCode) { + throw SocketTimeoutException("timeout") + } + res + } + .addInterceptor(logging) + .addInterceptor { chain -> + val request = chain.request() + try { + chain.proceed(request) + } catch (e: SocketTimeoutException) { + Response.Builder() + .code(timeoutCode) + .protocol(Protocol.HTTP_2) + .body(ResponseBody.create(MediaType.parse("text/plain"), "")) + .message("") + .request(request) + .build() + } + } try { val retrofit =