Record and Pass Correct Network Error (#7662)

This commit is contained in:
rahul bhat
2023-08-30 11:01:33 +05:30
committed by GitHub
parent 4a4ba0ad13
commit 3f77268bc3
4 changed files with 25 additions and 5 deletions

View File

@@ -21,6 +21,11 @@ import okhttp3.Response
import okhttp3.ResponseBody
import okio.Buffer
/**
* Interceptor to add encryption hash to the request.
* Don't add this at last in the interceptor chain because if requests fails,
* it will throw common status code 20 for all the requests.
*/
class EncryptionHashInterceptor(private val context: Context) : Interceptor {
init {

View File

@@ -12,6 +12,7 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Build
import android.provider.Settings
@@ -85,7 +86,17 @@ object BaseUtils {
fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
return connectivityManager?.activeNetworkInfo?.isConnected.orFalse()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network = connectivityManager?.activeNetwork ?: return false
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
return when {
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
}
} else {
return connectivityManager?.activeNetworkInfo?.isConnected.orFalse()
}
}
fun isFirebaseTokenRefreshed(): Boolean {

View File

@@ -57,8 +57,6 @@ abstract class BaseHttpClient(
connectTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
writeTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
readTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
addInterceptor(headerInterceptor)
addInterceptor(GzipInterceptor())
if (shouldAddAuthenticator) {
authenticator(TokenAuthenticator.getInstance())
}
@@ -66,6 +64,8 @@ abstract class BaseHttpClient(
certificatePinner(getCertificatePinner(networkInfo.baseUrl))
}
addInterceptor(EncryptionHashInterceptor(context))
addInterceptor(headerInterceptor)
addInterceptor(GzipInterceptor())
connectionPool(ConnectionPool(0, 5, TimeUnit.MINUTES))
.protocols(listOf(Protocol.HTTP_1_1))
}

View File

@@ -6,6 +6,7 @@ import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor
import com.navi.base.security.interceptor.EncryptionHashInterceptor
import com.navi.base.utils.BaseUtils
import com.navi.common.network.handleException
import com.navi.payment.BuildConfig
import com.navi.payment.network.util.ApiConstants
import com.navi.payment.utils.Constants.PAYMENT_SDK_VERSION
@@ -26,8 +27,8 @@ class NaviHttpClient(private val networkInfo: NetworkInfo,
connectTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
writeTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
readTimeout(networkInfo.timeoutInSec, TimeUnit.SECONDS)
addInterceptor(headerInterceptor)
addInterceptor(EncryptionHashInterceptor(context))
addInterceptor(headerInterceptor)
connectionPool(ConnectionPool(0, 5, TimeUnit.MINUTES))
.protocols(listOf(Protocol.HTTP_1_1))
}
@@ -71,11 +72,14 @@ class NaviHttpClient(private val networkInfo: NetworkInfo,
try {
chain.proceed(request)
} catch (e: IOException) {
val error = handleException(e)
// A mocked response in case of n/w exception
val errorStatusCode: Int = error.statusCode ?: ApiConstants.API_CODE_ERROR
// A mocked response in case of n/w exception
Response.Builder()
.request(request)
.protocol(Protocol.HTTP_2)
.code(ApiConstants.API_CODE_ERROR)
.code(errorStatusCode)
.message(e.message.orEmpty())
.body(ResponseBody.create("application/json".toMediaTypeOrNull(), "{}"))
.addHeader("content-type", "application/json")