Skip to content
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

Pass responses appropriately to errors (swaggerpy) #158

Merged
merged 1 commit into from
Jul 7, 2015
Merged
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
14 changes: 11 additions & 3 deletions swaggerpy/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ def handle_response_errors(e):
:raises HTTPError: :class: `swaggerpy.exception.HTTPError`
"""
args = list(e.args)
if hasattr(e, 'response') and hasattr(e.response, 'text'):
args[0] += (' : ' + e.response.text)
raise HTTPError(*args), None, sys.exc_info()[2]
kwargs = {}

if hasattr(e, 'response'):
kwargs['response'] = e.response
if hasattr(e.response, 'text'):
args[0] += (' : ' + e.response.text)

if hasattr(e, 'request'):
kwargs['request'] = e.request

raise HTTPError(*args, **kwargs), None, sys.exc_info()[2]


class HTTPFuture(object):
Expand Down
22 changes: 20 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,23 @@ 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"}')
response.json.return_value = {'foo': 'bar'}
error = Mock(
args=('400 Bad Request',), response=response, request=request
)
try:
handle_response_errors(error)
except HTTPError as e:
self.assertEqual(e.response.json(), {'foo': 'bar'})
self.assertEqual(e.response, response)
self.assertEqual(e.request, request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if there is a test already that '400 Bad Request' is contained in the e's args, will be good to add it if not present already.



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