Skip to content

Commit 33a653b

Browse files
Merge pull request #243 from ehdsouza/master
Generic error handling for 4xx when the response is plaintext
2 parents 942d023 + 6c312e7 commit 33a653b

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ install:
1515
- pip install pylint
1616
- pip install codecov
1717
- pip install git+git://github.com/watson-developer-cloud/python-sdk.git#egg=watson-developer-cloud
18+
- pip show six
1819
script:
1920
- sh pylint.sh
2021
- py.test test --cov=watson_developer_cloud

test/test_conversation_v1.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616
import responses
1717
import watson_developer_cloud
18+
from watson_developer_cloud import WatsonException
1819

1920
platform_url = 'https://gateway.watsonplatform.net'
2021
service_path = '/conversation/api'
@@ -48,6 +49,46 @@ def test_create_counterexample():
4849
assert responses.calls[0].request.url.startswith(url)
4950
assert counterexample == response
5051

52+
@responses.activate
53+
def test_rate_limit_exceeded():
54+
endpoint = '/v1/workspaces/{0}/counterexamples'.format('boguswid')
55+
url = '{0}{1}'.format(base_url, endpoint)
56+
error_code = "'code': '407'"
57+
error_msg = 'Rate limit exceeded'
58+
responses.add(
59+
responses.POST,
60+
url,
61+
body='Rate limit exceeded',
62+
status=407,
63+
content_type='application/json')
64+
service = watson_developer_cloud.ConversationV1(
65+
username='username', password='password', version='2017-02-03')
66+
try:
67+
service.create_counterexample(
68+
workspace_id='boguswid', text='I want financial advice today.')
69+
except WatsonException as ex:
70+
assert len(responses.calls) == 1
71+
assert error_code in str(ex)
72+
assert error_msg in str(ex)
73+
74+
@responses.activate
75+
def test_unknown_error():
76+
endpoint = '/v1/workspaces/{0}/counterexamples'.format('boguswid')
77+
url = '{0}{1}'.format(base_url, endpoint)
78+
error_msg = 'Unknown error'
79+
responses.add(
80+
responses.POST,
81+
url,
82+
status=407,
83+
content_type='application/json')
84+
service = watson_developer_cloud.ConversationV1(
85+
username='username', password='password', version='2017-02-03')
86+
try:
87+
service.create_counterexample(
88+
workspace_id='boguswid', text='I want financial advice today.')
89+
except WatsonException as ex:
90+
assert len(responses.calls) == 1
91+
assert error_msg in str(ex)
5192

5293
@responses.activate
5394
def test_delete_counterexample():

watson_developer_cloud/watson_developer_cloud_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ def _get_error_message(response):
190190
if 'description' in error_json:
191191
error_message += ', Description: ' + error_json['description']
192192
error_message += ', Code: ' + str(response.status_code)
193+
return error_message
193194
except:
194-
pass
195-
return error_message
195+
return {'error': response.text or error_message, 'code': str(response.status_code)}
196+
196197

197198
def _alchemy_html_request(self, method_name=None, url=None, html=None,
198199
text=None, params=None, method='POST',

0 commit comments

Comments
 (0)