Skip to content

Commit 62855d4

Browse files
committed
Properly throw MsalServiceError exception
1 parent db889c7 commit 62855d4

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

msal/exceptions.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@
2727

2828
class MsalError(Exception):
2929
# Define the template in Unicode to accommodate possible Unicode variables
30-
msg = u'An unspecified error'
30+
msg = u'An unspecified error' # Keeping for backward compatibility
3131

32-
def __init__(self, *args, **kwargs):
33-
super(MsalError, self).__init__(self.msg.format(**kwargs), *args)
34-
self.kwargs = kwargs
3532

3633
class MsalServiceError(MsalError):
37-
msg = u"{error}: {error_description}"
34+
msg = u"{error}: {error_description}" # Keeping for backward compatibility
35+
def __init__(
36+
self,
37+
*args,
38+
error: str, error_description: str, # Historically required, keeping them for now
39+
# 1. We can't simply remove them, or else it will be a breaking change
40+
# 2. We may change them to optional without breaking anyone. However,
41+
# such a change will be a one-way change, because once being optional,
42+
# we will never be able to change them (back) to be required.
43+
# 3. Since they were required and already exist anyway,
44+
# now we just keep them required "for now",
45+
# just in case that we would use them again.
46+
# There is no plan to do #1; and we keep option #2 open; we go with #3.
47+
**kwargs,
48+
):
49+
super().__init__(*args, **kwargs)
50+
self._error = error
51+
self._error_description = error_description
3852

msal/throttled_http_client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def __init__(self, raw_response):
6666
# self.raise_for_status = raw_response.raise_for_status
6767
def raise_for_status(self):
6868
if self.status_code >= 400:
69-
raise MsalServiceError("HTTP Error: {}".format(self.status_code))
69+
raise MsalServiceError(
70+
"HTTP Error: {}".format(self.status_code),
71+
error=None, error_description=None, # Historically required, keeping them for now
72+
)
7073

7174

7275
class ThrottledHttpClientBase(object):

tests/test_throttled_http_client.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from msal.throttled_http_client import (
88
ThrottledHttpClientBase, ThrottledHttpClient, NormalizedResponse)
9+
from msal.exceptions import MsalServiceError
910

1011
from tests import unittest
1112
from tests.http_client import MinimalResponse as _MinimalResponse
@@ -65,6 +66,26 @@ class CloseMethodCalled(Exception):
6566
pass
6667

6768

69+
class NormalizedResponseTestCase(unittest.TestCase):
70+
def test_pickled_minimal_response_should_contain_signature(self):
71+
self.assertIn(MinimalResponse.SIGNATURE, pickle.dumps(MinimalResponse(
72+
status_code=200, headers={}, text="foo")))
73+
74+
def test_normalized_response_should_not_contain_signature(self):
75+
response = NormalizedResponse(MinimalResponse(
76+
status_code=200, headers={}, text="foo"))
77+
self.assertNotIn(
78+
MinimalResponse.SIGNATURE, pickle.dumps(response),
79+
"A pickled object should not contain undesirable data")
80+
self.assertEqual(response.text, "foo", "Should return the same response text")
81+
82+
def test_normalized_response_raise_for_status_should_raise(self):
83+
response = NormalizedResponse(MinimalResponse(
84+
status_code=400, headers={}, text="foo"))
85+
with self.assertRaises(MsalServiceError):
86+
response.raise_for_status()
87+
88+
6889
class ThrottledHttpClientBaseTestCase(unittest.TestCase):
6990

7091
def assertCleanPickle(self, obj):
@@ -77,10 +98,6 @@ def assertValidResponse(self, response):
7798
self.assertIsInstance(response, NormalizedResponse)
7899
self.assertCleanPickle(response)
79100

80-
def test_pickled_minimal_response_should_contain_signature(self):
81-
self.assertIn(MinimalResponse.SIGNATURE, pickle.dumps(MinimalResponse(
82-
status_code=200, headers={}, text="foo")))
83-
84101
def test_throttled_http_client_base_response_should_tolerate_headerless_response(self):
85102
http_client = ThrottledHttpClientBase(DummyHttpClientWithoutResponseHeaders(
86103
status_code=200, response_text="foo"))

0 commit comments

Comments
 (0)