Skip to content

Commit

Permalink
fix: Watch() raises exceptions for received errors (#151)
Browse files Browse the repository at this point in the history
fix: Watch() raises exceptions for received errors
  • Loading branch information
tomplus authored Jul 16, 2021
1 parent 8f02233 commit 89e0e6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion kubernetes_asyncio/watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def unmarshal_event(self, data: str, response_type):
# not send a conventional ADDED/DELETED/... event but an error. Turn
# this error into a Python exception to save the user the hassle.
if js['type'].lower() == 'error':
return js
obj = js['raw_object']
reason = "%s: %s" % (obj['reason'], obj['message'])
raise client.exceptions.ApiException(status=obj['code'], reason=reason)

# If possible, compile the JSON response into a Python native response
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...
Expand Down
11 changes: 6 additions & 5 deletions kubernetes_asyncio/watch/watch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ async def test_unmarshall_k8s_error_response(self):
'kind': 'Status', 'apiVersion': 'v1', 'metadata': {},
'status': 'Failure',
'message': 'too old resource version: 1 (8146471)',
'reason': 'Gone', 'code': 410
'reason': 'Gone',
'code': 410
}
}

ret = Watch().unmarshal_event(json.dumps(k8s_err), None)
self.assertEqual(ret['type'], k8s_err['type'])
self.assertEqual(ret['object'], k8s_err['object'])
self.assertEqual(ret['object'], k8s_err['object'])
with self.assertRaisesRegex(
kubernetes_asyncio.client.exceptions.ApiException,
r'\(410\)\nReason: Gone: too old resource version: 1 \(8146471\)'):
Watch().unmarshal_event(json.dumps(k8s_err), None)

def test_unmarshal_with_custom_object(self):
w = Watch()
Expand Down

0 comments on commit 89e0e6a

Please sign in to comment.