Skip to content

Improve exception error messages #1219

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

Merged
merged 1 commit into from
Mar 27, 2018
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
21 changes: 21 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/ErrorCodes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.firebase.ui.auth;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -50,4 +52,23 @@ public final class ErrorCodes {
private ErrorCodes() {
throw new AssertionError("No instance for you!");
}

@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static String toFriendlyMessage(@Code int code) {
switch (code) {
case UNKNOWN_ERROR:
return "Unknown error";
case NO_NETWORK:
return "No internet connection";
case PLAY_SERVICES_UPDATE_CANCELLED:
return "Play Services update cancelled";
case DEVELOPER_ERROR:
return "Developer error";
case PROVIDER_ERROR:
return "Provider error";
default:
throw new IllegalArgumentException("Unknown code: " + code);
}
}
}
13 changes: 6 additions & 7 deletions auth/src/main/java/com/firebase/ui/auth/FirebaseUiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FirebaseUiException extends Exception {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public FirebaseUiException(@ErrorCodes.Code int code) {
mErrorCode = code;
this(code, ErrorCodes.toFriendlyMessage(code));
}

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

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull Throwable cause) {
this(code, ErrorCodes.toFriendlyMessage(code), cause);
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public FirebaseUiException(@ErrorCodes.Code int code,
@NonNull String message,
Expand All @@ -28,12 +33,6 @@ public FirebaseUiException(@ErrorCodes.Code int code,
mErrorCode = code;
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public FirebaseUiException(@ErrorCodes.Code int code, @NonNull Throwable cause) {
super(cause);
mErrorCode = code;
}

/**
* @return error code associated with this exception
* @see com.firebase.ui.auth.ErrorCodes
Expand Down
10 changes: 8 additions & 2 deletions auth/src/main/java/com/firebase/ui/auth/IdpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public static IdpResponse from(@NonNull Exception e) {
if (e instanceof FirebaseUiException) {
return new IdpResponse((FirebaseUiException) e);
} else {
return new IdpResponse(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, e));
FirebaseUiException wrapped = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, e);
wrapped.setStackTrace(e.getStackTrace());
return new IdpResponse(wrapped);
}
}

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