1# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2#
3# SPDX-License-Identifier: GPL-2.0-or-later
4
5from pkcs11.exceptions import (
6    AlreadyInitialized,
7    AnotherUserAlreadyLoggedIn,
8    ArgumentsBad,
9    DeviceRemoved,
10    DomainParamsInvalid,
11    FunctionFailed,
12    MechanismInvalid,
13    NoSuchKey,
14    NoSuchToken,
15    OperationNotInitialized,
16    SessionClosed,
17)
18
19
20def handle_exceptions(e, info=""):
21    exception_type = e.__class__
22    if exception_type == MechanismInvalid:
23        print("The External HSM does not support the given mechanism", info)
24    elif exception_type == FunctionFailed:
25        print(
26            "Please ensure proper configuration, privileges and environment variables"
27        )
28    elif exception_type == AlreadyInitialized:
29        print("pkcs11 is already initialized with another library")
30    elif exception_type == AnotherUserAlreadyLoggedIn:
31        print("Another User has been already logged in")
32    elif exception_type == ArgumentsBad:
33        print("Please check the arguments supplied to the function")
34    elif exception_type == DomainParamsInvalid:
35        print("Invalid or unsupported domain parameters were supplied to the function")
36    elif exception_type == DeviceRemoved:
37        print(
38            "The token has been removed from its slot during "
39            "the execution of the function"
40        )
41    elif exception_type == NoSuchToken:
42        print("No such token found")
43    elif exception_type == NoSuchKey:
44        print("No such key found")
45    elif exception_type == OperationNotInitialized:
46        print("Operation not Initialized")
47    elif exception_type == SessionClosed:
48        print("Session already closed")
49    else:
50        print(e.__class__, info)
51