Skip to content

Commit c9d7438

Browse files
committed
Add javadocs
1 parent 6817ef2 commit c9d7438

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/TaskUtils.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ interface Operation<TResult> {
2929
TResult run() throws FirebaseAppDistributionException;
3030
}
3131

32+
/**
33+
* Runs a long running operation inside a {@link Task}, wrapping any errors in {@link
34+
* FirebaseAppDistributionException}.
35+
*
36+
* <p>This allows long running operations to be chained together using {@link Task#onSuccessTask}.
37+
* If the operation throws an exception, the task will fail, and the exception will be surfaced to
38+
* the user as {@code FirebaseAppDistributionException}, available via {@link Task#getException}.
39+
*
40+
* <p>Exceptions that are not {@code FirebaseAppDistributionException} will be wrapped in one,
41+
* with {@link Status#UNKNOWN}.
42+
*
43+
* @param executor the executor in which to run the long running operation
44+
* @param operation the long running operation
45+
* @param <TResult> the type of the value returned by the operation
46+
* @return the task encompassing the long running operation
47+
*/
3248
static <TResult> Task<TResult> runAsyncInTask(Executor executor, Operation<TResult> operation) {
3349
TaskCompletionSource<TResult> taskCompletionSource = new TaskCompletionSource<>();
3450
executor.execute(
@@ -38,29 +54,41 @@ static <TResult> Task<TResult> runAsyncInTask(Executor executor, Operation<TResu
3854
} catch (FirebaseAppDistributionException e) {
3955
taskCompletionSource.setException(e);
4056
} catch (Throwable t) {
41-
taskCompletionSource.setException(
42-
new FirebaseAppDistributionException(
43-
String.format("%s: %s", ErrorMessages.UNKNOWN_ERROR, t.getMessage()),
44-
Status.UNKNOWN,
45-
t));
57+
taskCompletionSource.setException(wrapException(t));
4658
}
4759
});
4860
return taskCompletionSource.getTask();
4961
}
5062

63+
/**
64+
* Handle a {@link Task} that may fail with unexpected exceptions, wrapping them in {@link
65+
* FirebaseAppDistributionException}.
66+
*
67+
* <p>Chain this off of a task that may fail with unexpected exceptions, using {@link
68+
* Task#continueWithTask}. If the task fails, the task returned by this method will also fail,
69+
* with a {@code FirebaseAppDistributionException} of {@link Status#UNKNOWN} that wraps the
70+
* original exception.
71+
*
72+
* @param task the task that might fail with an unexpected exception
73+
* @param <TResult> the type of the value returned by the task
74+
* @return the new task that will fail with {@link FirebaseAppDistributionException}
75+
*/
5176
static <TResult> Task<TResult> handleTaskFailure(Task<TResult> task) {
5277
if (task.isComplete() && !task.isSuccessful()) {
5378
Exception e = task.getException();
5479
LogWrapper.getInstance().e(TAG + "Task failed to complete due to " + e.getMessage(), e);
55-
if (e instanceof FirebaseAppDistributionException) {
56-
return task;
57-
}
58-
return Tasks.forException(
59-
new FirebaseAppDistributionException(ErrorMessages.UNKNOWN_ERROR, Status.UNKNOWN, e));
80+
return e instanceof FirebaseAppDistributionException
81+
? task
82+
: Tasks.forException(wrapException(e));
6083
}
6184
return task;
6285
}
6386

87+
private static FirebaseAppDistributionException wrapException(Throwable t) {
88+
return new FirebaseAppDistributionException(
89+
String.format("%s: %s", ErrorMessages.UNKNOWN_ERROR, t.getMessage()), Status.UNKNOWN, t);
90+
}
91+
6492
static void safeSetTaskException(TaskCompletionSource taskCompletionSource, Exception e) {
6593
if (taskCompletionSource != null && !taskCompletionSource.getTask().isComplete()) {
6694
taskCompletionSource.setException(e);

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/NewReleaseFetcherTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,15 @@ public void checkForNewRelease_fisFailure() throws Exception {
183183
FirebaseAppDistributionException actualException =
184184
assertThrows(FirebaseAppDistributionException.class, onCompleteListener::await);
185185

186-
assertEquals(ErrorMessages.UNKNOWN_ERROR, actualException.getMessage());
187-
assertEquals(Status.UNKNOWN, actualException.getErrorCode());
188-
assertEquals(expectedException, actualException.getCause());
186+
assertThat(actualException).hasMessageThat().contains(ErrorMessages.UNKNOWN_ERROR);
187+
assertThat(actualException).hasMessageThat().contains("test ex");
188+
assertThat(actualException.getErrorCode()).isEqualTo(Status.UNKNOWN);
189+
assertThat(actualException).hasCauseThat().isEqualTo(expectedException);
189190
}
190191

191192
@Test
192193
public void checkForNewRelease_uncaughtExceptionFailure() throws Exception {
193-
RuntimeException expectedException = new RuntimeException("error");
194+
RuntimeException expectedException = new RuntimeException("test ex");
194195
when(mockFirebaseAppDistributionTesterApiClient.fetchNewRelease(
195196
any(), any(), any(), any(), any()))
196197
.thenThrow(expectedException);
@@ -203,7 +204,8 @@ public void checkForNewRelease_uncaughtExceptionFailure() throws Exception {
203204
FirebaseAppDistributionException actualException =
204205
assertThrows(FirebaseAppDistributionException.class, onCompleteListener::await);
205206

206-
assertThat(actualException.getMessage()).contains(ErrorMessages.UNKNOWN_ERROR);
207+
assertThat(actualException).hasMessageThat().contains(ErrorMessages.UNKNOWN_ERROR);
208+
assertThat(actualException).hasMessageThat().contains("test ex");
207209
assertThat(actualException.getErrorCode()).isEqualTo(Status.UNKNOWN);
208210
assertThat(actualException).hasCauseThat().isEqualTo(expectedException);
209211
}

0 commit comments

Comments
 (0)