From c38ef44c6872908e66029043b0ccb9623f523174 Mon Sep 17 00:00:00 2001 From: Sai Venkat Desu Date: Wed, 24 Jul 2024 12:23:55 +0530 Subject: [PATCH] chore: updated error adapters to set the cause as NetworkErrorException in both AuthenticationAPIClient & UsersAPIClient --- .../authentication/AuthenticationAPIClient.kt | 16 +++++++++++++++- .../authentication/AuthenticationException.kt | 10 ---------- .../auth0/android/management/UsersAPIClient.kt | 10 ++++++++++ .../AuthenticationExceptionTest.kt | 2 +- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt index eeb144a13..0cebc846d 100755 --- a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt +++ b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt @@ -3,6 +3,7 @@ package com.auth0.android.authentication import androidx.annotation.VisibleForTesting import com.auth0.android.Auth0 import com.auth0.android.Auth0Exception +import com.auth0.android.NetworkErrorException import com.auth0.android.request.* import com.auth0.android.request.internal.* import com.auth0.android.request.internal.GsonAdapter.Companion.forMap @@ -15,6 +16,9 @@ import com.google.gson.Gson import okhttp3.HttpUrl.Companion.toHttpUrl import java.io.IOException import java.io.Reader +import java.net.SocketException +import java.net.SocketTimeoutException +import java.net.UnknownHostException import java.security.PublicKey /** @@ -741,7 +745,11 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe val credentialsAdapter: JsonAdapter = GsonAdapter( Credentials::class.java, gson ) - val request = BaseAuthenticationRequest(factory.post(url.toString(), credentialsAdapter), clientId, baseURL) + val request = BaseAuthenticationRequest( + factory.post(url.toString(), credentialsAdapter), + clientId, + baseURL + ) request.addParameters(requestParameters) return request } @@ -811,6 +819,12 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe } override fun fromException(cause: Throwable): AuthenticationException { + if (cause is UnknownHostException || cause is SocketTimeoutException || cause is SocketException) { + return AuthenticationException( + "Failed to execute the network request", + NetworkErrorException(cause) + ) + } return AuthenticationException( "Something went wrong", Auth0Exception("Something went wrong", cause) diff --git a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationException.kt b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationException.kt index 652204db4..c0ec90419 100644 --- a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationException.kt +++ b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationException.kt @@ -4,10 +4,7 @@ import android.text.TextUtils import android.util.Log import com.auth0.android.Auth0Exception import com.auth0.android.NetworkErrorException -import java.net.SocketTimeoutException -import java.net.UnknownHostException import com.auth0.android.provider.TokenValidationException -import java.net.SocketException public class AuthenticationException : Auth0Exception { private var code: String? = null @@ -107,15 +104,8 @@ public class AuthenticationException : Auth0Exception { } // When the request failed due to network issues - // Currently [NetworkErrorException] is not properly thrown from [createErrorAdapter] in - // [AuthenticationAPIClient] and [UserAPIClient]. This will be fixed in the next major to avoid - // breaking change in the current major. We are not using IOException to check for the error - // since it is too broad. public val isNetworkError: Boolean get() = cause is NetworkErrorException - || cause?.cause is UnknownHostException - || cause?.cause is SocketTimeoutException - || cause?.cause is SocketException // When there is no Browser app installed to handle the web authentication public val isBrowserAppNotAvailable: Boolean diff --git a/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt b/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt index fca4fa6dd..88b47bd5e 100755 --- a/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt +++ b/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt @@ -3,6 +3,7 @@ package com.auth0.android.management import androidx.annotation.VisibleForTesting import com.auth0.android.Auth0 import com.auth0.android.Auth0Exception +import com.auth0.android.NetworkErrorException import com.auth0.android.authentication.ParameterBuilder import com.auth0.android.request.ErrorAdapter import com.auth0.android.request.JsonAdapter @@ -20,6 +21,9 @@ import com.google.gson.Gson import okhttp3.HttpUrl.Companion.toHttpUrl import java.io.IOException import java.io.Reader +import java.net.SocketException +import java.net.SocketTimeoutException +import java.net.UnknownHostException /** * API client for Auth0 Management API. @@ -219,6 +223,12 @@ public class UsersAPIClient @VisibleForTesting(otherwise = VisibleForTesting.PRI } override fun fromException(cause: Throwable): ManagementException { + if (cause is UnknownHostException || cause is SocketTimeoutException || cause is SocketException) { + return ManagementException( + "Failed to execute the network request", + NetworkErrorException(cause) + ) + } return ManagementException( "Something went wrong", Auth0Exception("Something went wrong", cause) diff --git a/auth0/src/test/java/com/auth0/android/authentication/AuthenticationExceptionTest.kt b/auth0/src/test/java/com/auth0/android/authentication/AuthenticationExceptionTest.kt index 97c37343b..e1bb2cfb9 100644 --- a/auth0/src/test/java/com/auth0/android/authentication/AuthenticationExceptionTest.kt +++ b/auth0/src/test/java/com/auth0/android/authentication/AuthenticationExceptionTest.kt @@ -205,7 +205,7 @@ public class AuthenticationExceptionTest { @Test public fun shouldHaveNetworkErrorForSocketTimeout() { val ex = AuthenticationException( - "Request has definitely failed", Auth0Exception("", + "Request has definitely failed", NetworkErrorException( SocketTimeoutException() ) )