• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.textclassifier.downloader;
18 
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import androidx.annotation.IntDef;
22 import java.lang.annotation.Retention;
23 
24 // TODO(licha): Consider making this a checked exception
25 /** Exception thrown when downloading a model. */
26 final class ModelDownloadException extends RuntimeException {
27 
28   // Consistent with TextClassifierDownloadReported.failure_reason. [1, 8, 9] reserved
29   public static final int UNKNOWN_FAILURE_REASON = 0;
30   public static final int FAILED_TO_DOWNLOAD_SERVICE_CONN_BROKEN = 2;
31   public static final int FAILED_TO_DOWNLOAD_404_ERROR = 3;
32   public static final int FAILED_TO_DOWNLOAD_OTHER = 4;
33   public static final int DOWNLOADED_FILE_MISSING = 5;
34   public static final int FAILED_TO_PARSE_MANIFEST = 6;
35   public static final int FAILED_TO_VALIDATE_MODEL = 7;
36 
37   /** Error code for a failed download task. */
38   @Retention(SOURCE)
39   @IntDef({
40     UNKNOWN_FAILURE_REASON,
41     FAILED_TO_DOWNLOAD_SERVICE_CONN_BROKEN,
42     FAILED_TO_DOWNLOAD_404_ERROR,
43     FAILED_TO_DOWNLOAD_OTHER,
44     DOWNLOADED_FILE_MISSING,
45     FAILED_TO_PARSE_MANIFEST,
46     FAILED_TO_VALIDATE_MODEL
47   })
48   public @interface ErrorCode {}
49 
50   public static final int DEFAULT_DOWNLOADER_LIB_ERROR_CODE = -1;
51 
52   private final int errorCode;
53 
54   private final int downloaderLibErrorCode;
55 
ModelDownloadException(@rrorCode int errorCode, Throwable cause)56   public ModelDownloadException(@ErrorCode int errorCode, Throwable cause) {
57     super(cause);
58     this.errorCode = errorCode;
59     this.downloaderLibErrorCode = DEFAULT_DOWNLOADER_LIB_ERROR_CODE;
60   }
61 
ModelDownloadException(@rrorCode int errorCode, String message)62   public ModelDownloadException(@ErrorCode int errorCode, String message) {
63     super(message);
64     this.errorCode = errorCode;
65     this.downloaderLibErrorCode = DEFAULT_DOWNLOADER_LIB_ERROR_CODE;
66   }
67 
ModelDownloadException( @rrorCode int errorCode, int downloaderLibErrorCode, String message)68   public ModelDownloadException(
69       @ErrorCode int errorCode, int downloaderLibErrorCode, String message) {
70     super(message);
71     this.errorCode = errorCode;
72     this.downloaderLibErrorCode = downloaderLibErrorCode;
73   }
74 
75   /** Returns the error code from Model Downloader itself. */
76   @ErrorCode
getErrorCode()77   public int getErrorCode() {
78     return errorCode;
79   }
80 
81   /** Returns the error code from internal HTTP stack. */
getDownloaderLibErrorCode()82   public int getDownloaderLibErrorCode() {
83     return downloaderLibErrorCode;
84   }
85 }
86