Skip to content

Properly throw MsalServiceError exception #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions msal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@

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

def __init__(self, *args, **kwargs):
super(MsalError, self).__init__(self.msg.format(**kwargs), *args)
self.kwargs = kwargs

class MsalServiceError(MsalError):
msg = u"{error}: {error_description}"
msg = u"{error}: {error_description}" # Keeping for backward compatibility
def __init__(
self,
*args,
error: str, error_description: str, # Historically required, keeping them for now
**kwargs,
):
super().__init__(*args, **kwargs)
self._error = error
self._error_description = error_description

5 changes: 4 additions & 1 deletion msal/throttled_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def __init__(self, raw_response):
# self.raise_for_status = raw_response.raise_for_status
def raise_for_status(self):
if self.status_code >= 400:
raise MsalServiceError("HTTP Error: {}".format(self.status_code))
raise MsalServiceError(
"HTTP Error: {}".format(self.status_code),
error=None, error_description=None, # Historically required, keeping them for now
)


class ThrottledHttpClientBase(object):
Expand Down
25 changes: 21 additions & 4 deletions tests/test_throttled_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from msal.throttled_http_client import (
ThrottledHttpClientBase, ThrottledHttpClient, NormalizedResponse)
from msal.exceptions import MsalServiceError

from tests import unittest
from tests.http_client import MinimalResponse as _MinimalResponse
Expand Down Expand Up @@ -65,6 +66,26 @@ class CloseMethodCalled(Exception):
pass


class NormalizedResponseTestCase(unittest.TestCase):
def test_pickled_minimal_response_should_contain_signature(self):
self.assertIn(MinimalResponse.SIGNATURE, pickle.dumps(MinimalResponse(
status_code=200, headers={}, text="foo")))

def test_normalized_response_should_not_contain_signature(self):
response = NormalizedResponse(MinimalResponse(
status_code=200, headers={}, text="foo"))
self.assertNotIn(
MinimalResponse.SIGNATURE, pickle.dumps(response),
"A pickled object should not contain undesirable data")
self.assertEqual(response.text, "foo", "Should return the same response text")

def test_normalized_response_raise_for_status_should_raise(self):
response = NormalizedResponse(MinimalResponse(
status_code=400, headers={}, text="foo"))
with self.assertRaises(MsalServiceError):
response.raise_for_status()


class ThrottledHttpClientBaseTestCase(unittest.TestCase):

def assertCleanPickle(self, obj):
Expand All @@ -77,10 +98,6 @@ def assertValidResponse(self, response):
self.assertIsInstance(response, NormalizedResponse)
self.assertCleanPickle(response)

def test_pickled_minimal_response_should_contain_signature(self):
self.assertIn(MinimalResponse.SIGNATURE, pickle.dumps(MinimalResponse(
status_code=200, headers={}, text="foo")))

def test_throttled_http_client_base_response_should_tolerate_headerless_response(self):
http_client = ThrottledHttpClientBase(DummyHttpClientWithoutResponseHeaders(
status_code=200, response_text="foo"))
Expand Down