Skip to content

Commit 5582982

Browse files
committed
Also catch the frequent AttributeError
1 parent 0f5fe25 commit 5582982

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

msal/broker.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
import logging
77
import uuid
88

9+
10+
logger = logging.getLogger(__name__)
911
try:
1012
import pymsalruntime # ImportError would be raised on unsupported platforms such as Windows 8
1113
# Its API description is available in site-packages/pymsalruntime/PyMsalRuntime.pyi
12-
except ImportError:
14+
pymsalruntime.set_logging_callback(lambda message, level: { # New in pymsalruntime 0.5.0
15+
pymsalruntime.LogLevel.TRACE: logger.debug, # Python has no TRACE level
16+
pymsalruntime.LogLevel.DEBUG: logger.debug,
17+
# Let broker's excess info, warning and error logs map into default DEBUG, for now
18+
#pymsalruntime.LogLevel.INFO: logger.info,
19+
#pymsalruntime.LogLevel.WARNING: logger.warning,
20+
#pymsalruntime.LogLevel.ERROR: logger.error,
21+
pymsalruntime.LogLevel.FATAL: logger.critical,
22+
}.get(level, logger.debug)(message))
23+
except (ImportError, AttributeError): # AttributeError happens when a prior pymsalruntime uninstallation somehow leaved an empty folder behind
1324
# PyMsalRuntime currently supports these Windows versions, listed in this MSFT internal link
1425
# https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/2406/files
1526
raise ImportError( # TODO: Remove or adjust this line right before merging this PR
@@ -18,18 +29,6 @@
1829
# Other exceptions (possibly RuntimeError) would be raised if its initialization fails
1930

2031

21-
logger = logging.getLogger(__name__)
22-
pymsalruntime.set_logging_callback(lambda message, level: { # New in pymsalruntime 0.5.0
23-
pymsalruntime.LogLevel.TRACE: logger.debug, # Python has no TRACE level
24-
pymsalruntime.LogLevel.DEBUG: logger.debug,
25-
# Let broker's excess info, warning and error logs map into default DEBUG, for now
26-
#pymsalruntime.LogLevel.INFO: logger.info,
27-
#pymsalruntime.LogLevel.WARNING: logger.warning,
28-
#pymsalruntime.LogLevel.ERROR: logger.error,
29-
pymsalruntime.LogLevel.FATAL: logger.critical,
30-
}.get(level, logger.debug)(message))
31-
32-
3332
class RedirectUriError(ValueError):
3433
pass
3534

0 commit comments

Comments
 (0)