Skip to content

Commit a85b2e0

Browse files
committed
fix: Watch() raises exceptions for received errors (#151)
fix: Watch() raises exceptions for received errors
1 parent 6ecacb5 commit a85b2e0

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

kubernetes_asyncio/watch/watch.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def unmarshal_event(self, data: str, response_type):
9393
# not send a conventional ADDED/DELETED/... event but an error. Turn
9494
# this error into a Python exception to save the user the hassle.
9595
if js['type'].lower() == 'error':
96-
return js
96+
obj = js['raw_object']
97+
reason = "%s: %s" % (obj['reason'], obj['message'])
98+
raise client.exceptions.ApiException(status=obj['code'], reason=reason)
9799

98100
# If possible, compile the JSON response into a Python native response
99101
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...

kubernetes_asyncio/watch/watch_test.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,15 @@ async def test_unmarshall_k8s_error_response(self):
157157
'kind': 'Status', 'apiVersion': 'v1', 'metadata': {},
158158
'status': 'Failure',
159159
'message': 'too old resource version: 1 (8146471)',
160-
'reason': 'Gone', 'code': 410
160+
'reason': 'Gone',
161+
'code': 410
161162
}
162163
}
163164

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

169170
def test_unmarshal_with_custom_object(self):
170171
w = Watch()

0 commit comments

Comments
 (0)