From 8d07f5d381ba55138fb6472e3f66cf84f0ff213b Mon Sep 17 00:00:00 2001 From: Benjamin Picolo Date: Tue, 7 Jul 2015 13:22:12 -0700 Subject: [PATCH] Pass responses appropriately to errors --- swaggerpy/response.py | 6 +++++- tests/resource_response_test.py | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/swaggerpy/response.py b/swaggerpy/response.py index 9a5dadbd..66ad7ffe 100644 --- a/swaggerpy/response.py +++ b/swaggerpy/response.py @@ -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): diff --git a/tests/resource_response_test.py b/tests/resource_response_test.py index 960400e3..6a59e880 100644 --- a/tests/resource_response_test.py +++ b/tests/resource_response_test.py @@ -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 @@ -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 = {