1 /* 2 * Copyright 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.credentials; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.os.CancellationSignal; 23 import android.os.OutcomeReceiver; 24 25 import com.android.internal.util.Preconditions; 26 27 import java.util.concurrent.Executor; 28 29 /** 30 * Represents an error encountered during the 31 * {@link CredentialManager#getCredential(Context, GetCredentialRequest, 32 * CancellationSignal, Executor, OutcomeReceiver)} operation. 33 */ 34 public class GetCredentialException extends Exception { 35 /** 36 * The error type value for when the given operation failed due to an unknown reason. 37 */ 38 @NonNull 39 public static final String TYPE_UNKNOWN = 40 "android.credentials.GetCredentialException.TYPE_UNKNOWN"; 41 42 /** 43 * The error type value for when no credential is found available for the given {@link 44 * CredentialManager#getCredential(Context, GetCredentialRequest, CancellationSignal, 45 * Executor, OutcomeReceiver)} request. 46 */ 47 @NonNull 48 public static final String TYPE_NO_CREDENTIAL = 49 "android.credentials.GetCredentialException.TYPE_NO_CREDENTIAL"; 50 /** 51 * The error type value for when the user intentionally cancelled the request. 52 * 53 * <p>This is a strong indicator that your app should refrain from making the same api call for 54 * a certain amount of time to provide a better user experience. 55 */ 56 @NonNull 57 public static final String TYPE_USER_CANCELED = 58 "android.credentials.GetCredentialException.TYPE_USER_CANCELED"; 59 /** 60 * The error type value for when the given operation failed due to internal interruption. 61 * Retrying the same operation should fix the error. 62 */ 63 @NonNull 64 public static final String TYPE_INTERRUPTED = 65 "android.credentials.GetCredentialException.TYPE_INTERRUPTED"; 66 67 @NonNull 68 private final String mType; 69 70 /** Returns the specific exception type. */ 71 @NonNull getType()72 public String getType() { 73 return mType; 74 } 75 76 /** 77 * Constructs a {@link GetCredentialException}. 78 * 79 * @throws IllegalArgumentException If type is empty. 80 */ GetCredentialException(@onNull String type, @Nullable String message)81 public GetCredentialException(@NonNull String type, @Nullable String message) { 82 this(type, message, null); 83 } 84 85 /** 86 * Constructs a {@link GetCredentialException}. 87 * 88 * @throws IllegalArgumentException If type is empty. 89 */ GetCredentialException( @onNull String type, @Nullable String message, @Nullable Throwable cause)90 public GetCredentialException( 91 @NonNull String type, @Nullable String message, @Nullable Throwable cause) { 92 super(message, cause); 93 this.mType = Preconditions.checkStringNotEmpty(type, 94 "type must not be empty"); 95 } 96 97 /** 98 * Constructs a {@link GetCredentialException}. 99 * 100 * @throws IllegalArgumentException If type is empty. 101 */ GetCredentialException(@onNull String type, @Nullable Throwable cause)102 public GetCredentialException(@NonNull String type, @Nullable Throwable cause) { 103 this(type, null, cause); 104 } 105 106 /** 107 * Constructs a {@link GetCredentialException}. 108 * 109 * @throws IllegalArgumentException If type is empty. 110 */ GetCredentialException(@onNull String type)111 public GetCredentialException(@NonNull String type) { 112 this(type, null, null); 113 } 114 } 115