diff --git a/kubernetes_asyncio/watch/watch.py b/kubernetes_asyncio/watch/watch.py index 324f0f465..fbc04270a 100644 --- a/kubernetes_asyncio/watch/watch.py +++ b/kubernetes_asyncio/watch/watch.py @@ -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`, ... diff --git a/kubernetes_asyncio/watch/watch_test.py b/kubernetes_asyncio/watch/watch_test.py index 99738207a..7ea9edc34 100644 --- a/kubernetes_asyncio/watch/watch_test.py +++ b/kubernetes_asyncio/watch/watch_test.py @@ -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()