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.Hide;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 
23 import com.android.internal.util.Preconditions;
24 
25 /**
26  * Represents an error encountered during the
27  * {@link CredentialManager#getCandidateCredentials} operation.
28  *
29  * @hide
30  */
31 @Hide
32 public class GetCandidateCredentialsException extends Exception {
33     /**
34      * The error type value for when the given operation failed due to an unknown reason.
35      */
36     @NonNull
37     public static final String TYPE_UNKNOWN =
38             "android.credentials.GetCandidateCredentialsException.TYPE_UNKNOWN";
39 
40     /**
41      * The error type value for when no credential is found available for the given {@link
42      * CredentialManager#getCandidateCredentials} request.
43      */
44     @NonNull
45     public static final String TYPE_NO_CREDENTIAL =
46             "android.credentials.GetCandidateCredentialsException.TYPE_NO_CREDENTIAL";
47 
48     @NonNull
49     public static final String TYPE_USER_CANCELED =
50             "android.credentials.GetCredentialException.TYPE_USER_CANCELED";
51     /**
52      * The error type value for when the given operation failed due to internal interruption.
53      * Retrying the same operation should fix the error.
54      */
55     @NonNull
56     public static final String TYPE_INTERRUPTED =
57             "android.credentials.GetCredentialException.TYPE_INTERRUPTED";
58 
59     @NonNull
60     private final String mType;
61 
62     /** Returns the specific exception type. */
63     @NonNull
getType()64     public String getType() {
65         return mType;
66     }
67 
68     /**
69      * Constructs a {@link GetCandidateCredentialsException}.
70      *
71      * @throws IllegalArgumentException If type is empty.
72      */
GetCandidateCredentialsException(@onNull String type, @Nullable String message)73     public GetCandidateCredentialsException(@NonNull String type, @Nullable String message) {
74         this(type, message, null);
75     }
76 
77     /**
78      * Constructs a {@link GetCandidateCredentialsException}.
79      *
80      * @throws IllegalArgumentException If type is empty.
81      */
GetCandidateCredentialsException( @onNull String type, @Nullable String message, @Nullable Throwable cause)82     public GetCandidateCredentialsException(
83             @NonNull String type, @Nullable String message, @Nullable Throwable cause) {
84         super(message, cause);
85         this.mType = Preconditions.checkStringNotEmpty(type,
86                 "type must not be empty");
87     }
88 
89     /**
90      * Constructs a {@link GetCandidateCredentialsException}.
91      *
92      * @throws IllegalArgumentException If type is empty.
93      */
GetCandidateCredentialsException(@onNull String type, @Nullable Throwable cause)94     public GetCandidateCredentialsException(@NonNull String type, @Nullable Throwable cause) {
95         this(type, null, cause);
96     }
97 
98     /**
99      * Constructs a {@link GetCandidateCredentialsException}.
100      *
101      * @throws IllegalArgumentException If type is empty.
102      */
GetCandidateCredentialsException(@onNull String type)103     public GetCandidateCredentialsException(@NonNull String type) {
104         this(type, null, null);
105     }
106 }
107