Skip to content

Commit 299788e

Browse files
SUPERCILEXsamtstern
authored andcommitted
Give clients much more detailed errors (#1168)
1 parent 039701a commit 299788e

17 files changed

+156
-112
lines changed

app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.support.design.widget.Snackbar;
2424
import android.support.v7.app.AppCompatActivity;
2525
import android.support.v7.app.AppCompatDelegate;
26+
import android.util.Log;
2627
import android.view.View;
2728
import android.widget.Button;
2829
import android.widget.CheckBox;
@@ -48,6 +49,8 @@
4849
import butterknife.OnClick;
4950

5051
public class AuthUiActivity extends AppCompatActivity {
52+
private static final String TAG = "AuthUiActivity";
53+
5154
private static final String GOOGLE_TOS_URL = "https://www.google.com/policies/terms/";
5255
private static final String FIREBASE_TOS_URL = "https://firebase.google.com/terms/";
5356
private static final String GOOGLE_PRIVACY_POLICY_URL = "https://www.google.com/policies/privacy/";
@@ -237,7 +240,6 @@ private void handleSignInResponse(int resultCode, Intent data) {
237240
if (resultCode == RESULT_OK) {
238241
startSignedInActivity(response);
239242
finish();
240-
return;
241243
} else {
242244
// Sign in failed
243245
if (response == null) {
@@ -246,18 +248,14 @@ private void handleSignInResponse(int resultCode, Intent data) {
246248
return;
247249
}
248250

249-
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
251+
if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
250252
showSnackbar(R.string.no_internet_connection);
251253
return;
252254
}
253255

254-
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
255-
showSnackbar(R.string.unknown_error);
256-
return;
257-
}
256+
showSnackbar(R.string.unknown_error);
257+
Log.e(TAG, "Sign-in error: ", response.getError());
258258
}
259-
260-
showSnackbar(R.string.unknown_sign_in_response);
261259
}
262260

263261
private void startSignedInActivity(IdpResponse response) {

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
<string name="require_name">Require first/last name with email accounts.</string>
6363

6464
<string name="unknown_response">Unexpected onActivityResult response code</string>
65-
<string name="unknown_sign_in_response">Unknown response from AuthUI sign-in</string>
6665
<string name="sign_in_cancelled">Sign in cancelled</string>
6766
<string name="no_internet_connection">No internet connection</string>
6867
<string name="unknown_error">An unknown error occurred</string>
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
package com.firebase.ui.auth;
22

3+
import android.support.annotation.IntDef;
4+
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
38
/**
4-
* Error codes retrieved from {@link IdpResponse#getErrorCode()}.
9+
* Error codes for failed sign-in attempts.
510
*/
611
public final class ErrorCodes {
712
/**
8-
* Sign in failed due to lack of network connection
9-
**/
10-
public static final int NO_NETWORK = 10;
13+
* Valid codes that can be returned from {@link FirebaseUiException#getErrorCode()}.
14+
*/
15+
@IntDef({
16+
UNKNOWN_ERROR,
17+
NO_NETWORK,
18+
PLAY_SERVICES_UPDATE_CANCELLED,
19+
DEVELOPER_ERROR
20+
})
21+
@Retention(RetentionPolicy.SOURCE)
22+
public @interface Code {}
23+
24+
/**
25+
* An unknown error has occurred.
26+
*/
27+
public static final int UNKNOWN_ERROR = 0;
28+
29+
/**
30+
* Sign in failed due to lack of network connection.
31+
*/
32+
public static final int NO_NETWORK = 1;
33+
34+
/**
35+
* A required update to Play Services was cancelled by the user.
36+
*/
37+
public static final int PLAY_SERVICES_UPDATE_CANCELLED = 2;
1138

1239
/**
13-
* An unknown error has occurred
14-
**/
15-
public static final int UNKNOWN_ERROR = 20;
40+
* A sign-in operation couldn't be completed due to a developer error.
41+
*/
42+
public static final int DEVELOPER_ERROR = 3;
1643

1744
private ErrorCodes() {
18-
// no instance
45+
throw new AssertionError("No instance for you!");
1946
}
2047
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.firebase.ui.auth;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.RestrictTo;
5+
6+
/**
7+
* Base class for all FirebaseUI exceptions.
8+
*/
9+
public class FirebaseUiException extends Exception {
10+
private final int mErrorCode;
11+
12+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
13+
public FirebaseUiException(@ErrorCodes.Code int code) {
14+
mErrorCode = code;
15+
}
16+
17+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
18+
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull String message) {
19+
super(message);
20+
mErrorCode = code;
21+
}
22+
23+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
24+
public FirebaseUiException(@ErrorCodes.Code int code,
25+
@NonNull String message,
26+
@NonNull Throwable cause) {
27+
super(message, cause);
28+
mErrorCode = code;
29+
}
30+
31+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
32+
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull Throwable cause) {
33+
super(cause);
34+
mErrorCode = code;
35+
}
36+
37+
/**
38+
* @return error code associated with this exception
39+
* @see com.firebase.ui.auth.ErrorCodes
40+
*/
41+
@ErrorCodes.Code
42+
public final int getErrorCode() {
43+
return mErrorCode;
44+
}
45+
}

auth/src/main/java/com/firebase/ui/auth/IdpResponse.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import android.support.annotation.RestrictTo;
2424
import android.text.TextUtils;
2525

26-
import com.firebase.ui.auth.data.model.FirebaseUiException;
2726
import com.firebase.ui.auth.data.model.User;
2827
import com.firebase.ui.auth.util.ExtraConstants;
2928
import com.google.firebase.auth.GoogleAuthProvider;
@@ -78,17 +77,17 @@ public static IdpResponse fromResultIntent(@Nullable Intent resultIntent) {
7877
}
7978

8079
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
81-
public static IdpResponse fromError(@NonNull FirebaseUiException e) {
82-
return new IdpResponse(e);
80+
public static Intent getErrorIntent(@NonNull Exception e) {
81+
return fromError(e).toIntent();
8382
}
8483

85-
/**
86-
* @deprecated migrate internals to {@link #fromError(FirebaseUiException)}
87-
*/
88-
@Deprecated
8984
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
90-
public static Intent getErrorCodeIntent(int errorCode) {
91-
return new IdpResponse(new FirebaseUiException(errorCode)).toIntent();
85+
public static IdpResponse fromError(@NonNull Exception e) {
86+
if (e instanceof FirebaseUiException) {
87+
return new IdpResponse((FirebaseUiException) e);
88+
} else {
89+
return new IdpResponse(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, e));
90+
}
9291
}
9392

9493
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@@ -149,7 +148,10 @@ public String getIdpSecret() {
149148

150149
/**
151150
* Get the error code for a failed sign in
151+
*
152+
* @deprecated use {@link #getError()} instead
152153
*/
154+
@Deprecated
153155
public int getErrorCode() {
154156
if (isSuccessful()) {
155157
return Activity.RESULT_OK;
@@ -158,9 +160,11 @@ public int getErrorCode() {
158160
}
159161
}
160162

163+
/**
164+
* Get the error for a failed sign in.
165+
*/
161166
@Nullable
162-
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
163-
public FirebaseUiException getException() {
167+
public FirebaseUiException getError() {
164168
return mException;
165169
}
166170

auth/src/main/java/com/firebase/ui/auth/KickoffActivity.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ protected void onCreate(Bundle savedInstance) {
3333
if (savedInstance == null || savedInstance.getBoolean(IS_WAITING_FOR_PLAY_SERVICES)) {
3434
if (isOffline()) {
3535
Log.d(TAG, "No network connection");
36-
finish(RESULT_CANCELED,
37-
IdpResponse.getErrorCodeIntent(ErrorCodes.NO_NETWORK));
36+
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(
37+
new FirebaseUiException(ErrorCodes.NO_NETWORK)));
3838
return;
3939
}
4040

@@ -44,9 +44,8 @@ protected void onCreate(Bundle savedInstance) {
4444
new DialogInterface.OnCancelListener() {
4545
@Override
4646
public void onCancel(DialogInterface dialog) {
47-
finish(RESULT_CANCELED,
48-
IdpResponse.getErrorCodeIntent(
49-
ErrorCodes.UNKNOWN_ERROR));
47+
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(
48+
new FirebaseUiException(ErrorCodes.PLAY_SERVICES_UPDATE_CANCELLED)));
5049
}
5150
});
5251

@@ -73,8 +72,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
7372
if (resultCode == RESULT_OK) {
7473
start();
7574
} else {
76-
finish(RESULT_CANCELED,
77-
IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR));
75+
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(
76+
new FirebaseUiException(ErrorCodes.PLAY_SERVICES_UPDATE_CANCELLED)));
7877
}
7978
} else {
8079
SignInDelegate delegate = SignInDelegate.getInstance(this);

auth/src/main/java/com/firebase/ui/auth/data/model/FirebaseUiException.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

auth/src/main/java/com/firebase/ui/auth/provider/FacebookProvider.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import com.facebook.login.LoginManager;
3535
import com.facebook.login.LoginResult;
3636
import com.firebase.ui.auth.AuthUI;
37+
import com.firebase.ui.auth.ErrorCodes;
38+
import com.firebase.ui.auth.FirebaseUiException;
3739
import com.firebase.ui.auth.IdpResponse;
3840
import com.firebase.ui.auth.R;
3941
import com.firebase.ui.auth.data.model.User;
@@ -130,12 +132,12 @@ public void onCompleted(JSONObject object, GraphResponse response) {
130132
FacebookRequestError requestError = response.getError();
131133
if (requestError != null) {
132134
Log.e(TAG, "Received Facebook error: " + requestError.getErrorMessage());
133-
onFailure();
135+
onFailure(requestError.getException());
134136
return;
135137
}
136138
if (object == null) {
137139
Log.w(TAG, "Received null response from Facebook GraphRequest");
138-
onFailure();
140+
onFailure(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR));
139141
} else {
140142
String email = null;
141143
String name = null;
@@ -168,13 +170,13 @@ public void onCompleted(JSONObject object, GraphResponse response) {
168170

169171
@Override
170172
public void onCancel() {
171-
onFailure();
173+
onFailure(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR));
172174
}
173175

174176
@Override
175-
public void onError(FacebookException error) {
176-
Log.e(TAG, "Error logging in with Facebook. " + error.getMessage());
177-
onFailure();
177+
public void onError(FacebookException e) {
178+
Log.e(TAG, "Error logging in with Facebook. " + e.getMessage());
179+
onFailure(e);
178180
}
179181

180182
private void onSuccess(LoginResult loginResult,
@@ -191,9 +193,9 @@ private void onSuccess(LoginResult loginResult,
191193
.build());
192194
}
193195

194-
private void onFailure() {
196+
private void onFailure(Exception e) {
195197
gcCallbackManager();
196-
mCallbackObject.onFailure();
198+
mCallbackObject.onFailure(e);
197199
}
198200

199201
private void gcCallbackManager() {

auth/src/main/java/com/firebase/ui/auth/provider/GoogleProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import android.widget.Toast;
2626

2727
import com.firebase.ui.auth.AuthUI.IdpConfig;
28+
import com.firebase.ui.auth.ErrorCodes;
29+
import com.firebase.ui.auth.FirebaseUiException;
2830
import com.firebase.ui.auth.IdpResponse;
2931
import com.firebase.ui.auth.R;
3032
import com.firebase.ui.auth.data.model.User;
@@ -152,7 +154,7 @@ private void onError(GoogleSignInResult result) {
152154

153155
private void onError(String errorMessage) {
154156
Log.e(TAG, "Error logging in with Google. " + errorMessage);
155-
mIdpCallback.onFailure();
157+
mIdpCallback.onFailure(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, errorMessage));
156158
}
157159
}
158160

auth/src/main/java/com/firebase/ui/auth/provider/IdpProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
package com.firebase.ui.auth.provider;
1616

17+
import android.support.annotation.NonNull;
18+
import android.support.annotation.RestrictTo;
19+
1720
import com.firebase.ui.auth.IdpResponse;
1821

22+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1923
public interface IdpProvider extends Provider {
2024
interface IdpCallback {
21-
void onSuccess(IdpResponse idpResponse);
25+
void onSuccess(@NonNull IdpResponse idpResponse);
2226

23-
void onFailure();
27+
void onFailure(@NonNull Exception e);
2428
}
2529

2630
void setAuthenticationCallback(IdpCallback callback);

auth/src/main/java/com/firebase/ui/auth/provider/Provider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.support.annotation.LayoutRes;
7+
import android.support.annotation.RestrictTo;
78

9+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
810
public interface Provider {
911
/** Retrieves the name of the IDP, for display on-screen. */
1012
String getName(Context context);

0 commit comments

Comments
 (0)