Skip to content

Commit

Permalink
chore: updated error adapters to set the cause as NetworkErrorExcepti…
Browse files Browse the repository at this point in the history
…on in both AuthenticationAPIClient & UsersAPIClient
  • Loading branch information
desusai7 committed Jul 24, 2024
1 parent e267ab6 commit c38ef44
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -741,7 +745,11 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
val credentialsAdapter: JsonAdapter<Credentials> = 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
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
)
Expand Down

0 comments on commit c38ef44

Please sign in to comment.