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

fix: Watch() raises exceptions for received errors #151

Merged
merged 3 commits into from
Jul 16, 2021
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
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