1 /* 2 * Copyright (C) 2022 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 android.adservices.ondevicepersonalization; 18 import android.annotation.FlaggedApi; 19 import android.annotation.IntDef; 20 import android.annotation.Nullable; 21 22 import com.android.adservices.ondevicepersonalization.flags.Flags; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 import java.util.Set; 27 28 /** 29 * Exception thrown by OnDevicePersonalization APIs. 30 * 31 */ 32 public class OnDevicePersonalizationException extends Exception { 33 /** 34 * The {@link IsolatedService} that was invoked failed to run. 35 */ 36 public static final int ERROR_ISOLATED_SERVICE_FAILED = 1; 37 38 /** 39 * The {@link IsolatedService} was not started because personalization is disabled by 40 * device configuration. 41 */ 42 public static final int ERROR_PERSONALIZATION_DISABLED = 2; 43 44 /** The ODP module was unable to load the {@link IsolatedService}. 45 * 46 * <p> Retrying may be successful for platform internal errors. 47 */ 48 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 49 public static final int ERROR_ISOLATED_SERVICE_LOADING_FAILED = 3; 50 51 /** 52 * The ODP specific manifest settings for the {@link IsolatedService} are either missing or 53 * misconfigured. 54 */ 55 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 56 public static final int ERROR_ISOLATED_SERVICE_MANIFEST_PARSING_FAILED = 4; 57 58 /** The {@link IsolatedService} was invoked but timed out before returning successfully. 59 * 60 * <p> This is likely due to an issue with the {@link IsolatedWorker} implementation taking too 61 * long and retries are likely to fail. 62 */ 63 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 64 public static final int ERROR_ISOLATED_SERVICE_TIMEOUT = 5; 65 66 /** The {@link IsolatedService}'s call to {@link FederatedComputeScheduler#schedule} failed. 67 * 68 <p> Retrying may be successful if the issue is due to a platform internal error. 69 */ 70 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 71 public static final int ERROR_SCHEDULE_TRAINING_FAILED = 6; 72 73 /** 74 * The {@link IsolatedService}'s call to {@link FederatedComputeScheduler#schedule} failed due 75 * to missing or misconfigured federated compute settings URL in the manifest. 76 */ 77 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 78 public static final int ERROR_INVALID_TRAINING_MANIFEST = 7; 79 80 /** Inference failed due to {@link ModelManager} not finding the downloaded model. */ 81 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 82 public static final int ERROR_INFERENCE_MODEL_NOT_FOUND = 8; 83 84 /** {@link ModelManager} failed to run inference. 85 * 86 <p> Retrying may be successful if the issue is due to a platform internal error. 87 */ 88 @FlaggedApi(Flags.FLAG_EXECUTE_IN_ISOLATED_SERVICE_API_ENABLED) 89 public static final int ERROR_INFERENCE_FAILED = 9; 90 91 /** @hide */ 92 private static final Set<Integer> VALID_ERROR_CODE = 93 Set.of( 94 ERROR_ISOLATED_SERVICE_FAILED, 95 ERROR_PERSONALIZATION_DISABLED, 96 ERROR_ISOLATED_SERVICE_LOADING_FAILED, 97 ERROR_ISOLATED_SERVICE_MANIFEST_PARSING_FAILED, 98 ERROR_ISOLATED_SERVICE_TIMEOUT, 99 ERROR_SCHEDULE_TRAINING_FAILED, 100 ERROR_INVALID_TRAINING_MANIFEST, 101 ERROR_INFERENCE_MODEL_NOT_FOUND, 102 ERROR_INFERENCE_FAILED); 103 104 /** @hide */ 105 @IntDef( 106 prefix = "ERROR_", 107 value = { 108 ERROR_ISOLATED_SERVICE_FAILED, 109 ERROR_PERSONALIZATION_DISABLED, 110 ERROR_ISOLATED_SERVICE_LOADING_FAILED, 111 ERROR_ISOLATED_SERVICE_MANIFEST_PARSING_FAILED, 112 ERROR_ISOLATED_SERVICE_TIMEOUT, 113 ERROR_SCHEDULE_TRAINING_FAILED, 114 ERROR_INVALID_TRAINING_MANIFEST, 115 ERROR_INFERENCE_MODEL_NOT_FOUND, 116 ERROR_INFERENCE_FAILED 117 }) 118 @Retention(RetentionPolicy.SOURCE) 119 public @interface ErrorCode {} 120 121 private final @ErrorCode int mErrorCode; 122 123 @FlaggedApi(Flags.FLAG_UNHIDDEN_ON_DEVICE_PERSONALIZATION_EXCEPTION_ENABLED) OnDevicePersonalizationException(@rrorCode int errorCode)124 public OnDevicePersonalizationException(@ErrorCode int errorCode) { 125 mErrorCode = errorCode; 126 } 127 128 @FlaggedApi(Flags.FLAG_UNHIDDEN_ON_DEVICE_PERSONALIZATION_EXCEPTION_ENABLED) OnDevicePersonalizationException( @rrorCode int errorCode, @Nullable String message)129 public OnDevicePersonalizationException( 130 @ErrorCode int errorCode, @Nullable String message) { 131 super(message); 132 mErrorCode = errorCode; 133 } 134 135 @FlaggedApi(Flags.FLAG_UNHIDDEN_ON_DEVICE_PERSONALIZATION_EXCEPTION_ENABLED) OnDevicePersonalizationException( @rrorCode int errorCode, @Nullable Throwable cause)136 public OnDevicePersonalizationException( 137 @ErrorCode int errorCode, @Nullable Throwable cause) { 138 super(cause); 139 mErrorCode = errorCode; 140 } 141 142 @FlaggedApi(Flags.FLAG_UNHIDDEN_ON_DEVICE_PERSONALIZATION_EXCEPTION_ENABLED) OnDevicePersonalizationException( @rrorCode int errorCode, @Nullable String message, @Nullable Throwable cause)143 public OnDevicePersonalizationException( 144 @ErrorCode int errorCode, @Nullable String message, @Nullable Throwable cause) { 145 super(message, cause); 146 mErrorCode = errorCode; 147 } 148 149 /** Returns the error code for this exception. */ getErrorCode()150 public @ErrorCode int getErrorCode() { 151 return mErrorCode; 152 } 153 154 /** 155 * @hide Only used by internal error code validation. 156 */ isValidErrorCode(int errorCode)157 public static boolean isValidErrorCode(int errorCode) { 158 return VALID_ERROR_CODE.contains(errorCode); 159 } 160 } 161