@@ -29,6 +29,22 @@ interface Operation<TResult> {
29
29
TResult run () throws FirebaseAppDistributionException ;
30
30
}
31
31
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
+ */
32
48
static <TResult > Task <TResult > runAsyncInTask (Executor executor , Operation <TResult > operation ) {
33
49
TaskCompletionSource <TResult > taskCompletionSource = new TaskCompletionSource <>();
34
50
executor .execute (
@@ -38,29 +54,41 @@ static <TResult> Task<TResult> runAsyncInTask(Executor executor, Operation<TResu
38
54
} catch (FirebaseAppDistributionException e ) {
39
55
taskCompletionSource .setException (e );
40
56
} 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 ));
46
58
}
47
59
});
48
60
return taskCompletionSource .getTask ();
49
61
}
50
62
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
+ */
51
76
static <TResult > Task <TResult > handleTaskFailure (Task <TResult > task ) {
52
77
if (task .isComplete () && !task .isSuccessful ()) {
53
78
Exception e = task .getException ();
54
79
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 ));
60
83
}
61
84
return task ;
62
85
}
63
86
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
+
64
92
static void safeSetTaskException (TaskCompletionSource taskCompletionSource , Exception e ) {
65
93
if (taskCompletionSource != null && !taskCompletionSource .getTask ().isComplete ()) {
66
94
taskCompletionSource .setException (e );
0 commit comments