Skip to content

Commit b90a6ec

Browse files
authored
Merge pull request #1219 from SUPERCILEX/error-messages
Improve exception error messages
2 parents 3ee1155 + 4f29572 commit b90a6ec

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.firebase.ui.auth;
22

33
import android.support.annotation.IntDef;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.RestrictTo;
46

57
import java.lang.annotation.Retention;
68
import java.lang.annotation.RetentionPolicy;
@@ -50,4 +52,23 @@ public final class ErrorCodes {
5052
private ErrorCodes() {
5153
throw new AssertionError("No instance for you!");
5254
}
55+
56+
@NonNull
57+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
58+
public static String toFriendlyMessage(@Code int code) {
59+
switch (code) {
60+
case UNKNOWN_ERROR:
61+
return "Unknown error";
62+
case NO_NETWORK:
63+
return "No internet connection";
64+
case PLAY_SERVICES_UPDATE_CANCELLED:
65+
return "Play Services update cancelled";
66+
case DEVELOPER_ERROR:
67+
return "Developer error";
68+
case PROVIDER_ERROR:
69+
return "Provider error";
70+
default:
71+
throw new IllegalArgumentException("Unknown code: " + code);
72+
}
73+
}
5374
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class FirebaseUiException extends Exception {
1111

1212
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
1313
public FirebaseUiException(@ErrorCodes.Code int code) {
14-
mErrorCode = code;
14+
this(code, ErrorCodes.toFriendlyMessage(code));
1515
}
1616

1717
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@@ -20,6 +20,11 @@ public FirebaseUiException(@ErrorCodes.Code int code, @NonNull String message) {
2020
mErrorCode = code;
2121
}
2222

23+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
24+
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull Throwable cause) {
25+
this(code, ErrorCodes.toFriendlyMessage(code), cause);
26+
}
27+
2328
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
2429
public FirebaseUiException(@ErrorCodes.Code int code,
2530
@NonNull String message,
@@ -28,12 +33,6 @@ public FirebaseUiException(@ErrorCodes.Code int code,
2833
mErrorCode = code;
2934
}
3035

31-
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
32-
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull Throwable cause) {
33-
super(cause);
34-
mErrorCode = code;
35-
}
36-
3736
/**
3837
* @return error code associated with this exception
3938
* @see com.firebase.ui.auth.ErrorCodes

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public static IdpResponse from(@NonNull Exception e) {
103103
if (e instanceof FirebaseUiException) {
104104
return new IdpResponse((FirebaseUiException) e);
105105
} else {
106-
return new IdpResponse(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, e));
106+
FirebaseUiException wrapped = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, e);
107+
wrapped.setStackTrace(e.getStackTrace());
108+
return new IdpResponse(wrapped);
107109
}
108110
}
109111

@@ -213,7 +215,11 @@ public void writeToParcel(Parcel dest, int flags) {
213215
} catch (IOException e) {
214216
// Somewhere down the line, the exception is holding on to an object that isn't
215217
// serializable so default to some exception. It's the best we can do in this case.
216-
dest.writeSerializable(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR));
218+
FirebaseUiException fake = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR,
219+
"Fake exception created, original: " + mException
220+
+ ", original cause: " + mException.getCause());
221+
fake.setStackTrace(mException.getStackTrace());
222+
dest.writeSerializable(fake);
217223
} finally {
218224
if (oos != null) {
219225
try {

0 commit comments

Comments
 (0)