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 androidx.credentials.exceptions.publickeycredential
18 
19 import androidx.annotation.RestrictTo
20 import androidx.credentials.exceptions.GetCredentialCustomException
21 import androidx.credentials.exceptions.GetCredentialException
22 import androidx.credentials.exceptions.domerrors.DomError
23 import androidx.credentials.exceptions.domerrors.UnknownError
24 import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils.Companion.SEPARATOR
25 import androidx.credentials.internal.FrameworkClassParsingException
26 
27 /**
28  * During the get-passkey flow, this is thrown when a DOM Exception is thrown, indicating the
29  * operation contains a DOMException error type. The fido spec can be found
30  * [here](https://webidl.spec.whatwg.org/#idl-DOMException-error-names). The full list of
31  * implemented DOMErrors extends from and can be seen at [DomError].
32  *
33  * @property domError the specific error from the DOMException types defined in the fido spec found
34  *   [here](https://webidl.spec.whatwg.org/#idl-DOMException-error-names)
35  * @throws NullPointerException If [domError] is null
36  */
37 class GetPublicKeyCredentialDomException
38 @JvmOverloads
39 constructor(val domError: DomError, errorMessage: CharSequence? = null) :
40     GetPublicKeyCredentialException(
41         TYPE_GET_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION + SEPARATOR + domError.type,
42         errorMessage
43     ) {
44     internal companion object {
45         internal const val TYPE_GET_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION: String =
46             "androidx.credentials.TYPE_GET_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION"
47 
48         @JvmStatic
49         @RestrictTo(RestrictTo.Scope.LIBRARY) // used from java tests
createFromnull50         fun createFrom(type: String, msg: String?): GetCredentialException {
51             val prefix = "$TYPE_GET_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION$SEPARATOR"
52             return try {
53                 DomExceptionUtils.generateDomException(
54                     type,
55                     prefix,
56                     msg,
57                     GetPublicKeyCredentialDomException(UnknownError())
58                 )
59             } catch (t: FrameworkClassParsingException) {
60                 // Parsing failed but don't crash the process. Instead just output a response
61                 // with the raw framework values.
62                 GetCredentialCustomException(type, msg)
63             }
64         }
65     }
66 }
67