Skip to content

Commit 47108e9

Browse files
authored
Chain exceptions from LibsecretPersistence (#20380)
1 parent 3fbb0f9 commit 47108e9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

sdk/identity/azure-identity/azure/identity/_persistent_cache.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import logging
56
import os
67
import sys
78
from typing import TYPE_CHECKING
89

10+
import six
11+
912
if TYPE_CHECKING:
1013
from typing import Any
1114
import msal_extensions
1215

16+
_LOGGER = logging.getLogger(__name__)
17+
1318

1419
class TokenCachePersistenceOptions(object):
1520
"""Options for persistent token caching.
@@ -86,12 +91,16 @@ def _get_persistence(allow_unencrypted, account_name, cache_name):
8691
return msal_extensions.LibsecretPersistence(
8792
file_path, cache_name, {"MsalClientID": "Microsoft.Developer.IdentityService"}, label=account_name
8893
)
89-
except ImportError:
94+
except Exception as ex: # pylint:disable=broad-except
95+
_LOGGER.debug('msal-extensions is unable to encrypt a persistent cache: "%s"', ex, exc_info=True)
9096
if not allow_unencrypted:
91-
raise ValueError(
92-
"PyGObject is required to encrypt the persistent cache. Please install that library or "
93-
+ 'specify "allow_unencrypted_storage=True" to store the cache without encryption.'
97+
error = ValueError(
98+
"Cache encryption is impossible because libsecret dependencies are not installed or are unusable,"
99+
+ " for example because no display is available (as in an SSH session). The chained exception has"
100+
+ ' more information. Specify "allow_unencrypted_storage=True" to store the cache unencrypted'
101+
+ " instead of raising this exception."
94102
)
95-
return msal_extensions.FilePersistence(file_path)
103+
six.raise_from(error, ex)
104+
return msal_extensions.FilePersistence(file_path)
96105

97106
raise NotImplementedError("A persistent cache is not available in this environment.")

0 commit comments

Comments
 (0)