From 631918f0a3c79c95fc7c05dfb57192943ec2d666 Mon Sep 17 00:00:00 2001 From: Christopher Wolfe Date: Fri, 6 Dec 2019 08:54:10 -0500 Subject: [PATCH] Changed the default value of self._simplify_error_response to match past behavior. If you set self._simplify_error_response to False, error responses with a JSON body will be deserialized and returned in their entirety. If you do nothing, error responses will be reduced down to the error message, as they have been for years. That seems less disruptive. --- README.rst | 16 +++++++++++++--- sprockets/mixins/http/__init__.py | 6 +++--- tests.py | 12 ++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 90087fb..c48f8bd 100644 --- a/README.rst +++ b/README.rst @@ -85,9 +85,19 @@ As with Tornado, to use the curl client which has numerous benefits: Error Response Body ------------------- -For errors, the HTTPResponse object will include a complete body. -To reduce error response bodies down to just the error message, users of this -mixin can set ``self.simplify_error_response = True``. +For errors, i.e. a response with HTTP status code in the 400 range... + +The HTTPResponse object's body is reduced down to just the error message. +That is this mixin's default behavior. + +For a JSON response body with Problem Details (RFC 7807), you may want more +than just the error message. To gain access to the complete, deserialized +response body; a class that uses this mixin can set: + +.. code:: python + + ``self.simplify_error_response = False``. + Environment Variables --------------------- diff --git a/sprockets/mixins/http/__init__.py b/sprockets/mixins/http/__init__.py index d0170b5..930eb75 100644 --- a/sprockets/mixins/http/__init__.py +++ b/sprockets/mixins/http/__init__.py @@ -31,14 +31,14 @@ class HTTPResponse: """ - def __init__(self, simplify_error_response=False): + def __init__(self, simplify_error_response=True): self._exceptions = [] self._finish = None self._json = transcoders.JSONTranscoder() self._msgpack = transcoders.MsgPackTranscoder() self._responses = [] self._start = time.time() - self._simplify_error_response = simplify_error_response or False + self._simplify_error_response = simplify_error_response def __len__(self): """Return the length of the exception stack and response stack. @@ -255,7 +255,7 @@ class HTTPClientMixin: super().__init__(*args, **kwargs) self.__hcm_json = transcoders.JSONTranscoder() self.__hcm_msgpack = transcoders.MsgPackTranscoder() - self.simplify_error_response = False + self.simplify_error_response = True async def http_fetch(self, url, method='GET', diff --git a/tests.py b/tests.py index e6bd856..d11c201 100644 --- a/tests.py +++ b/tests.py @@ -440,19 +440,19 @@ class MixinTestCase(testing.AsyncHTTPTestCase): self.assertFalse(response.ok) self.assertEqual(response.code, 400) self.assertEqual(response.attempts, 1) - self.assertEqual( - response.body, - {'message': 'Test Error', 'type': 'Test Error', 'traceback': None}) + self.assertEqual(response.body, 'Test Error') @testing.gen_test - def test_simpler_error_response(self): - self.mixin.simplify_error_response = True + def test_fancier_error_response(self): + self.mixin.simplify_error_response = False response = yield self.mixin.http_fetch( self.get_url('/error?status_code=400&message=Test%20Error')) self.assertFalse(response.ok) self.assertEqual(response.code, 400) self.assertEqual(response.attempts, 1) - self.assertEqual(response.body, 'Test Error') + self.assertEqual( + response.body, + {'message': 'Test Error', 'type': 'Test Error', 'traceback': None}) @testing.gen_test def test_error_retry(self):