Skip to content

Commit c005611

Browse files
committed
remove thrown exception - pass back as task exception.
1 parent 8db52f8 commit c005611

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/FirebaseModelDownloader.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public static FirebaseModelDownloader getInstance(@NonNull FirebaseApp app) {
121121
public Task<CustomModel> getModel(
122122
@NonNull String modelName,
123123
@NonNull DownloadType downloadType,
124-
@Nullable CustomModelDownloadConditions conditions)
125-
throws Exception {
124+
@Nullable CustomModelDownloadConditions conditions) {
126125
CustomModel localModelDetails = getLocalModelDetails(modelName);
127126
if (localModelDetails == null) {
128127
// no local model - get latest.
@@ -140,9 +139,10 @@ public Task<CustomModel> getModel(
140139
getCustomModelTask(modelName, conditions, localModelDetails.getModelHash());
141140
return getCompletedLocalCustomModelTask(localModelDetails);
142141
}
143-
throw new FirebaseMlException(
144-
"Unsupported downloadType, please chose LOCAL_MODEL, LATEST_MODEL, or LOCAL_MODEL_UPDATE_IN_BACKGROUND",
145-
FirebaseMlException.INVALID_ARGUMENT);
142+
return Tasks.forException(
143+
new FirebaseMlException(
144+
"Unsupported downloadType, please chose LOCAL_MODEL, LATEST_MODEL, or LOCAL_MODEL_UPDATE_IN_BACKGROUND",
145+
FirebaseMlException.INVALID_ARGUMENT));
146146
}
147147

148148
/**
@@ -228,8 +228,7 @@ private Task<CustomModel> getCompletedLocalCustomModelTask(@NonNull CustomModel
228228
// This version of getCustomModelTask will always call the modelDownloadService and upon
229229
// success will then trigger file download.
230230
private Task<CustomModel> getCustomModelTask(
231-
@NonNull String modelName, @Nullable CustomModelDownloadConditions conditions)
232-
throws Exception {
231+
@NonNull String modelName, @Nullable CustomModelDownloadConditions conditions) {
233232
return getCustomModelTask(modelName, conditions, null);
234233
}
235234

@@ -238,8 +237,7 @@ private Task<CustomModel> getCustomModelTask(
238237
private Task<CustomModel> getCustomModelTask(
239238
@NonNull String modelName,
240239
@Nullable CustomModelDownloadConditions conditions,
241-
@Nullable String modelHash)
242-
throws Exception {
240+
@Nullable String modelHash) {
243241
CustomModel currentModel = sharedPreferencesUtil.getCustomModelDetails(modelName);
244242
if (currentModel == null && modelHash != null) {
245243
// todo(annzimmer) log something about mismatched state and use hash = null
@@ -322,8 +320,7 @@ private Task<CustomModel> retryExpiredUrlDownload(
322320
@NonNull String modelName,
323321
@Nullable CustomModelDownloadConditions conditions,
324322
Task<Void> downloadTask,
325-
int retryCounter)
326-
throws Exception {
323+
int retryCounter) {
327324
if (downloadTask.getException().getMessage().contains("Retry: Expired URL")) {
328325
// this is likely an expired url - retry.
329326
Task<CustomModel> retryModelDetails =
@@ -357,7 +354,7 @@ private Task<CustomModel> retryExpiredUrlDownload(
357354
return Tasks.forException(new Exception("File download failed."));
358355
}
359356

360-
private Task<CustomModel> finishModelDownload(@NonNull String modelName) throws Exception {
357+
private Task<CustomModel> finishModelDownload(@NonNull String modelName) {
361358
// read the updated model
362359
CustomModel downloadedModel = sharedPreferencesUtil.getDownloadingCustomModelDetails(modelName);
363360
if (downloadedModel == null) {
@@ -385,11 +382,7 @@ private Task<CustomModel> finishModelDownload(@NonNull String modelName) throws
385382
@NonNull
386383
public Task<Set<CustomModel>> listDownloadedModels() {
387384
// trigger completion of file moves for download files.
388-
try {
389-
fileDownloadService.maybeCheckDownloadingComplete();
390-
} catch (Exception ex) {
391-
System.out.println("Error checking for in progress downloads: " + ex.getMessage());
392-
}
385+
fileDownloadService.maybeCheckDownloadingComplete();
393386

394387
TaskCompletionSource<Set<CustomModel>> taskCompletionSource = new TaskCompletionSource<>();
395388
executor.execute(

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/CustomModelDownloadService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ public CustomModelDownloadService(
100100
* @param projectNumber - firebase project number
101101
* @param modelName - model name
102102
* @return - updated model with new download url and expiry time
103-
* @throws Exception - errors when Firebase ML Download Service call fails.
104103
*/
105104
@NonNull
106-
public Task<CustomModel> getNewDownloadUrlWithExpiry(String projectNumber, String modelName)
107-
throws Exception {
105+
public Task<CustomModel> getNewDownloadUrlWithExpiry(String projectNumber, String modelName) {
108106
return getCustomModelDetails(projectNumber, modelName, null);
109107
}
110108

@@ -118,11 +116,10 @@ public Task<CustomModel> getNewDownloadUrlWithExpiry(String projectNumber, Strin
118116
* force retrieval of a new download url
119117
* @return The download details for the model or null if the current model hash matches the latest
120118
* model.
121-
* @throws Exception -errors when call to API fails.
122119
*/
123120
@NonNull
124121
public Task<CustomModel> getCustomModelDetails(
125-
String projectNumber, String modelName, String modelHash) throws Exception {
122+
String projectNumber, String modelName, String modelHash) {
126123
try {
127124
URL url =
128125
new URL(String.format(DOWNLOAD_MODEL_REGEX, downloadHost, projectNumber, modelName));

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private synchronized ParcelFileDescriptor getDownloadedFile(Long downloadingId)
319319
return fileDescriptor;
320320
}
321321

322-
public void maybeCheckDownloadingComplete() throws Exception {
322+
public void maybeCheckDownloadingComplete() {
323323
for (String key : sharedPreferencesUtil.getSharedPreferenceKeySet()) {
324324
// if a local file path is present - get model details.
325325
Matcher matcher =
@@ -341,7 +341,7 @@ public void maybeCheckDownloadingComplete() throws Exception {
341341

342342
@Nullable
343343
@WorkerThread
344-
public File loadNewlyDownloadedModelFile(CustomModel model) throws FirebaseMlException {
344+
public File loadNewlyDownloadedModelFile(CustomModel model) {
345345
if (model == null) {
346346
return null;
347347
}
@@ -380,6 +380,9 @@ public File loadNewlyDownloadedModelFile(CustomModel model) throws FirebaseMlExc
380380
try {
381381
// TODO add logging
382382
newModelFile = fileManager.moveModelToDestinationFolder(model, fileDescriptor);
383+
} catch (FirebaseMlException ex) {
384+
// add logging for this error
385+
newModelFile = null;
383386
} finally {
384387
removeOrCancelDownloadModel(model.getName(), model.getDownloadId());
385388
}

0 commit comments

Comments
 (0)