Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose NetworkErrorException when request fails due to networking #235

Merged
merged 3 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ If the `Auth0` instance wasn't configured as "OIDC conformant", this call requir
```java
authentication
.login("info@auth0.com", "a secret password", "my-database-connection")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
//Logged in!
Expand All @@ -329,7 +329,7 @@ When you sign in to a multifactor authentication enabled connection using the `l
```java
authentication
.loginWithOTP("the mfa token", "123456")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
//Logged in!
Expand All @@ -355,7 +355,7 @@ Step 1: Request the code
```java
authentication
.passwordlessWithEmail("info@auth0.com", PasswordlessType.CODE, "my-passwordless-connection")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<Void, AuthenticationException>() {
@Override
public void onSuccess(Void payload) {
//Code sent!
Expand All @@ -375,6 +375,7 @@ Step 2: Input the code
```java
authentication
.loginWithEmail("info@auth0.com", "123456", "my-passwordless-connection")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
//Logged in!
Expand All @@ -393,7 +394,7 @@ authentication
```java
authentication
.signUp("info@auth0.com", "a secret password", "my-database-connection")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
//Signed Up & Logged in!
Expand All @@ -412,7 +413,7 @@ authentication
```java
authentication
.userInfo("user access_token")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<UserProfile, AuthenticationException>() {
@Override
public void onSuccess(UserProfile payload) {
//Got the profile!
Expand Down Expand Up @@ -442,14 +443,14 @@ UsersAPIClient users = new UsersAPIClient(account, "api token");
```java
users
.link("primary user id", "secondary user token")
.start(new BaseCallback<List<UserIdentity>>() {
.start(new BaseCallback<List<UserIdentity>, ManagementException>() {
@Override
public void onSuccess(List<UserIdentity> payload) {
//Got the updated identities! Accounts linked.
}

@Override
public void onFailure(Auth0Exception error) {
public void onFailure(ManagementException error) {
//Error!
}
});
Expand All @@ -460,14 +461,14 @@ users
```java
users
.unlink("primary user id", "secondary user id", "secondary provider")
.start(new BaseCallback<List<UserIdentity>>() {
.start(new BaseCallback<List<UserIdentity>, ManagementException>() {
@Override
public void onSuccess(List<UserIdentity> payload) {
//Got the updated identities! Accounts linked.
}

@Override
public void onFailure(Auth0Exception error) {
public void onFailure(ManagementException error) {
//Error!
}
});
Expand All @@ -481,7 +482,7 @@ users
.start(new BaseCallback<UserProfile, ManagementException>() {
@Override
public void onSuccess(UserProfile payload) {
//Profile
//Profile received
}

@Override
Expand All @@ -503,7 +504,7 @@ users
.start(new BaseCallback<UserProfile, ManagementException>() {
@Override
public void onSuccess(UserProfile payload) {
//Metadata updated
//User Metadata updated
}

@Override
Expand Down Expand Up @@ -542,9 +543,9 @@ The credentials to save **must have** `expires_in` and at least an `access_token
authentication
.login("info@auth0.com", "a secret password", "my-database-connection")
.setScope("openid offline_access")
.start(new BaseCallback<Credentials>() {
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials credentials) {
public void onSuccess(Credentials payload) {
//Save the credentials
manager.saveCredentials(credentials);
}
Expand All @@ -567,14 +568,16 @@ boolean authenticated = manager.hasValidCredentials();
Existing credentials will be returned if they are still valid, otherwise the `refresh_token` will be used to attempt to renew them. If the `expires_in` or both the `access_token` and `id_token` values are missing, the method will throw a `CredentialsManagerException`. The same will happen if the credentials have expired and there's no `refresh_token` available.

```java
manager.getCredentials(new BaseCallback<Credentials, CredentialsManagerException>(){
public void onSuccess(Credentials credentials){
//Use the Credentials
}
manager.getCredentials(new BaseCallback<Credentials, CredentialsManagerException>() {
@Override
public void onSuccess(Credentials credentials){
//Use the Credentials
}

@Override
public void onFailure(CredentialsManagerException error){
//Error!
}
//Error!
}
});
```

Expand Down
11 changes: 11 additions & 0 deletions auth0/src/main/java/com/auth0/android/NetworkErrorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.auth0.android;

/**
* Exception that represents a failure caused when attempting to execute a network request
*/
public class NetworkErrorException extends Auth0Exception {

public NetworkErrorException(Throwable cause) {
super("Failed to execute the network request", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.util.Log;

import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -145,6 +146,11 @@ public Object getValue(String key) {
return values.get(key);
}

// When the request failed due to network issues
public boolean isNetworkError() {
return getCause() instanceof NetworkErrorException;
}

// When there is no Browser app installed to handle the web authentication
public boolean isBrowserAppNotAvailable() {
return "a0.browser_not_available".equals(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.auth0.android.management;

import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -115,4 +116,9 @@ public Object getValue(String key) {
return values.get(key);
}

// When the request failed due to network issues
public boolean isNetworkError() {
return getCause() instanceof NetworkErrorException;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import com.auth0.android.request.Request;
import com.auth0.android.request.internal.GsonProvider;
import com.auth0.android.request.internal.ManagementErrorBuilder;
import com.auth0.android.request.internal.OkHttpClientFactory;
import com.auth0.android.request.internal.RequestFactory;
import com.auth0.android.result.UserIdentity;
import com.auth0.android.result.UserProfile;
import com.auth0.android.request.internal.OkHttpClientFactory;
import com.auth0.android.util.Telemetry;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.support.annotation.VisibleForTesting;

import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;
import com.auth0.android.RequestBodyBuildException;
import com.auth0.android.authentication.ParameterBuilder;
import com.auth0.android.callback.BaseCallback;
Expand Down Expand Up @@ -147,8 +148,7 @@ protected U parseUnsuccessfulResponse(Response response) {

@Override
public void onFailure(Request request, IOException e) {
Auth0Exception exception = new Auth0Exception("Failed to execute request to " + url.toString(), e);
postOnFailure(errorBuilder.from("Request failed", exception));
postOnFailure(errorBuilder.from("Request failed", new NetworkErrorException(e)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.auth0.android.request.internal;

import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;
import com.auth0.android.RequestBodyBuildException;
import com.auth0.android.request.ErrorBuilder;
import com.auth0.android.request.ParameterizableRequest;
Expand Down Expand Up @@ -98,7 +99,7 @@ public T execute() throws Auth0Exception {
try {
response = client.newCall(request).execute();
} catch (IOException e) {
throw new Auth0Exception("Failed to execute request to " + url, e);
throw getErrorBuilder().from("Request failed", new NetworkErrorException(e));
}

if (!response.isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.android.authentication;

import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;
import com.auth0.android.request.internal.GsonProvider;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand All @@ -14,6 +15,7 @@
import org.robolectric.annotation.Config;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -138,6 +140,18 @@ public void shouldReturnNullIfMapDoesNotExist() throws Exception {
assertThat(ex4.getValue("key"), is(nullValue()));
}

@Test
public void shouldNotHaveNetworkError() {
AuthenticationException ex = new AuthenticationException("Something else happened");
assertThat(ex.isNetworkError(), is(false));
}

@Test
public void shouldHaveNetworkError() {
AuthenticationException ex = new AuthenticationException("Request has definitely failed", new NetworkErrorException(new IOException()));
assertThat(ex.isNetworkError(), is(true));
}

@Test
public void shouldHaveExpiredMultifactorTokenOnOIDCMode() throws Exception {
values.put(ERROR_KEY, "expired_token");
Expand Down Expand Up @@ -212,7 +226,8 @@ public void shouldHaveNotStrongPassword() throws Exception {
public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exception {
Gson gson = GsonProvider.buildGson();
FileReader fr = new FileReader(PASSWORD_STRENGTH_ERROR_RESPONSE);
Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> mapPayload = gson.fromJson(fr, mapType);

AuthenticationException ex = new AuthenticationException(mapPayload);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.auth0.android.management;

import com.auth0.android.NetworkErrorException;

import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ManagementExceptionTest {

@Test
public void shouldNotHaveNetworkError() {
ManagementException ex = new ManagementException("Something else happened");
assertThat(ex.isNetworkError(), is(false));
}

@Test
public void shouldHaveNetworkError() {
ManagementException ex = new ManagementException("Request has definitely failed", new NetworkErrorException(new IOException()));
assertThat(ex.isNetworkError(), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


import com.auth0.android.Auth0Exception;
import com.auth0.android.NetworkErrorException;
import com.auth0.android.RequestBodyBuildException;
import com.auth0.android.authentication.ParameterBuilder;
import com.auth0.android.callback.BaseCallback;
Expand Down Expand Up @@ -178,9 +179,9 @@ public void shouldPostOnFailure() throws Exception {
}

@Test
public void shouldBuildException() throws Exception {
public void shouldBuildNetworkErrorException() throws Exception {
baseRequest.onFailure(null, mock(IOException.class));
verify(errorBuilder).from(eq("Request failed"), any(Auth0Exception.class));
verify(errorBuilder).from(eq("Request failed"), any(NetworkErrorException.class));
}

@Test
Expand Down