Skip to content

Commit

Permalink
Pass responses appropriately to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Picolo committed Jul 7, 2015
1 parent da672bd commit 8d07f5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion swaggerpy/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def handle_response_errors(e):
:raises HTTPError: :class: `swaggerpy.exception.HTTPError`
"""
args = list(e.args)
kwargs = {}
if hasattr(e, 'response') and hasattr(e.response, 'text'):
args[0] += (' : ' + e.response.text)
raise HTTPError(*args), None, sys.exc_info()[2]
kwargs['response'] = e.response
if hasattr(e, 'request'):
kwargs['request'] = e.request
raise HTTPError(*args, **kwargs), None, sys.exc_info()[2]


class HTTPFuture(object):
Expand Down
21 changes: 19 additions & 2 deletions tests/resource_response_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@
"""

import datetime
from swaggerpy.compat import json
import unittest
from mock import Mock
from swaggerpy.compat import json

import httpretty
from dateutil.tz import tzutc
from mock import Mock

from swaggerpy.client import SwaggerClient
from swaggerpy.exception import CancelledError, HTTPError
from swaggerpy.processors import SwaggerError
from swaggerpy.response import handle_response_errors
from swaggerpy.response import HTTPFuture


Expand All @@ -87,6 +88,22 @@ def test_cancelled_returns_false_if_called_before_cancel(self):
self.assertFalse(self.future.cancelled())


class HandleResponseErrorsTest(unittest.TestCase):

def test_pass_response_and_request(self):
request = Mock()
response = Mock(text='{"foo": "bar"}')
error = Mock(
args=('400 Bad Request',), response=response, request=request
)
try:
handle_response_errors(error)
except HTTPError as e:
self.assertEqual(e.json, {'foo': 'bar'})
self.assertEqual(e.response, response)
self.assertEqual(e.request, request)


class ResourceResponseTest(unittest.TestCase):
def setUp(self):
parameter = {
Expand Down

0 comments on commit 8d07f5d

Please sign in to comment.