Skip to content

Commit 56d1eab

Browse files
authored
Propagating the exceptions to the clients. (#856)
* Propagating the exceptions to the clients. * Adding javadocs for StateListener class.
1 parent 58c8efd commit 56d1eab

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public void testGetAuthToken_PersistedFidError_failure() throws Exception {
386386
.isInstanceOf(FirebaseInstallationsException.class);
387387
assertWithMessage("Exception status doesn't match")
388388
.that(((FirebaseInstallationsException) expected.getCause()).getStatus())
389-
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
389+
.isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR);
390390
}
391391
}
392392

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ private void triggerOnStateReached(PersistedFidEntry persistedFidEntry) {
197197
}
198198
}
199199

200+
private void triggerOnException(PersistedFidEntry persistedFidEntry, Exception exception) {
201+
synchronized (lock) {
202+
Iterator<StateListener> it = listeners.iterator();
203+
while (it.hasNext()) {
204+
StateListener l = it.next();
205+
boolean doneListening = l.onException(persistedFidEntry, exception);
206+
if (doneListening) {
207+
it.remove();
208+
}
209+
}
210+
}
211+
}
212+
200213
private final void doRegistration() {
201214
try {
202215
PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue();
@@ -248,7 +261,7 @@ private final void doRegistration() {
248261
.setRegistrationStatus(RegistrationStatus.REGISTER_ERROR)
249262
.build();
250263
persistedFid.insertOrUpdatePersistedFidEntry(errorFidEntry);
251-
triggerOnStateReached(errorFidEntry);
264+
triggerOnException(errorFidEntry, e);
252265
}
253266
}
254267

firebase-installations/src/main/java/com/google/firebase/installations/GetAuthTokenListener.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ public boolean onStateReached(
4242
.build());
4343
return true;
4444
}
45+
return false;
46+
}
4547

48+
@Override
49+
public boolean onException(PersistedFidEntry persistedFidEntry, Exception exception) {
4650
if (persistedFidEntry.isErrored()) {
47-
resultTaskCompletionSource.setException(
48-
new FirebaseInstallationsException(
49-
"Firebase Installation is not registered.",
50-
FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR));
51+
resultTaskCompletionSource.trySetException(exception);
5152
return true;
5253
}
5354
return false;

firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ public GetIdListener(TaskCompletionSource<String> taskCompletionSource) {
2727
@Override
2828
public boolean onStateReached(PersistedFidEntry persistedFidEntry, boolean unused) {
2929
if (persistedFidEntry.isUnregistered() || persistedFidEntry.isRegistered()) {
30-
taskCompletionSource.setResult(persistedFidEntry.getFirebaseInstallationId());
30+
taskCompletionSource.trySetResult(persistedFidEntry.getFirebaseInstallationId());
3131
return true;
3232
}
33+
return false;
34+
}
3335

36+
@Override
37+
public boolean onException(PersistedFidEntry persistedFidEntry, Exception exception) {
3438
if (persistedFidEntry.isErrored()) {
35-
taskCompletionSource.setException(
36-
new FirebaseInstallationsException(
37-
"Failed to update client side cache.",
38-
FirebaseInstallationsException.Status.CLIENT_ERROR));
39+
taskCompletionSource.trySetException(exception);
3940
return true;
4041
}
4142
return false;

firebase-installations/src/main/java/com/google/firebase/installations/StateListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,15 @@
1717
import com.google.firebase.installations.local.PersistedFidEntry;
1818

1919
interface StateListener {
20+
/**
21+
* Returns {@code true} if the defined {@link PersistedFidEntry} state is reached, {@code false}
22+
* otherwise.
23+
*/
2024
boolean onStateReached(PersistedFidEntry persistedFidEntry, boolean shouldRefreshAuthToken);
25+
26+
/**
27+
* Returns {@code true} if an exception is thrown while registering a Firebase Installation,
28+
* {@code false} otherwise.
29+
*/
30+
boolean onException(PersistedFidEntry persistedFidEntry, Exception exception);
2131
}

0 commit comments

Comments
 (0)