Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit bd98fd4

Browse files
author
Fabian Reinartz
committed
Handle error events in watch
Raise an ApiException for error events that indicate a watch failure despite the HTTP response indicating success. Fixes #57 Signed-off-by: Fabian Reinartz <[email protected]>
1 parent 879ab01 commit bd98fd4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: watch/watch.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ def stream(self, func, *args, **kwargs):
128128
resp = func(*args, **kwargs)
129129
try:
130130
for line in iter_resp_lines(resp):
131-
yield self.unmarshal_event(line, return_type)
131+
evt = self.unmarshal_event(line, return_type)
132+
if evt['type'] == 'ERROR':
133+
obj = evt['raw_object']
134+
reason = "%s: %s" % (obj['reason'], obj['message'])
135+
raise client.rest.ApiException(status=obj['code'],
136+
reason=reason)
137+
yield evt
138+
132139
if self._stop:
133140
break
134141
finally:

Diff for: watch/watch_test.py

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from mock import Mock
1818

19+
from kubernetes import client
20+
1921
from .watch import Watch
2022

2123

@@ -173,6 +175,32 @@ def test_watch_with_exception(self):
173175
fake_resp.close.assert_called_once()
174176
fake_resp.release_conn.assert_called_once()
175177

178+
def test_watch_with_error_event(self):
179+
print("start")
180+
fake_resp = Mock()
181+
fake_resp.close = Mock()
182+
fake_resp.release_conn = Mock()
183+
fake_resp.read_chunked = Mock(
184+
return_value=[
185+
'{"type": "ERROR", "object": {"code": 410, '
186+
'"reason": "Gone", "message": "error message"}}\n'])
187+
188+
fake_api = Mock()
189+
fake_api.get_thing = Mock(return_value=fake_resp)
190+
191+
w = Watch()
192+
try:
193+
for _ in w.stream(fake_api.get_thing):
194+
self.fail(self, "Should fail with ApiException.")
195+
except client.rest.ApiException:
196+
pass
197+
198+
fake_api.get_thing.assert_called_once_with(
199+
_preload_content=False, watch=True)
200+
fake_resp.read_chunked.assert_called_once_with(decode_content=False)
201+
fake_resp.close.assert_called_once()
202+
fake_resp.release_conn.assert_called_once()
203+
176204

177205
if __name__ == '__main__':
178206
unittest.main()

0 commit comments

Comments
 (0)