Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
When gateway sends failure response, include text in raised exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Jerram authored and dims committed Sep 12, 2019
1 parent 7714b9d commit 483a37e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion etcd3gw/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def post(self, *args, **kwargs):
if resp.status_code in _EXCEPTIONS_BY_CODE:
raise _EXCEPTIONS_BY_CODE[resp.status_code](resp.reason)
if resp.status_code != requests.codes['ok']:
raise exceptions.Etcd3Exception(resp.reason)
raise exceptions.Etcd3Exception(resp.reason, resp.text)
except requests.exceptions.Timeout as ex:
raise exceptions.ConnectionTimeoutError(six.text_type(ex))
except requests.exceptions.ConnectionError as ex:
Expand Down
4 changes: 3 additions & 1 deletion etcd3gw/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@


class Etcd3Exception(Exception):
pass
def __init__(self, msg, detail_text=None):
super(Etcd3Exception, self).__init__(msg)
self.detail_text = detail_text


class WatchTimedOut(Etcd3Exception):
Expand Down
24 changes: 24 additions & 0 deletions etcd3gw/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock

from etcd3gw.client import Etcd3Client
from etcd3gw.exceptions import Etcd3Exception
from etcd3gw.tests import base


Expand All @@ -30,3 +33,24 @@ def test_client_ipv6(self):
client = Etcd3Client(host="::1")
self.assertEqual("http://[::1]:2379/v3alpha/lease/grant",
client.get_url("/lease/grant"))

def test_client_bad_request(self):
client = Etcd3Client(host="127.0.0.1")
with mock.patch.object(client, "session") as mock_session:
mock_response = mock.Mock()
mock_response.status_code = 400
mock_response.reason = "Bad Request"
mock_response.text = '''{
"error": "etcdserver: mvcc: required revision has been compacted",
"code": 11
}'''
mock_session.post.return_value = mock_response
try:
client.status()
self.assertFalse(True)
except Etcd3Exception as e:
self.assertEqual(str(e), "Bad Request")
self.assertEqual(e.detail_text, '''{
"error": "etcdserver: mvcc: required revision has been compacted",
"code": 11
}''')

0 comments on commit 483a37e

Please sign in to comment.