Handle failures in ansible-connection instead of returning None

pull/75313/head
Nathaniel Case 3 years ago committed by Kate Case
parent 6f65397871
commit bcf9b2ea3c

@ -0,0 +1,3 @@
---
bugfixes:
- ansible-connection - handle errors when passing methods to persistent connection instead of closing connection (https://github.com/ansible-collections/ansible.netcommon/issues/301).

@ -135,6 +135,7 @@ class ConnectionProcess(object):
self.exception = None self.exception = None
(s, addr) = self.sock.accept() (s, addr) = self.sock.accept()
try:
signal.alarm(0) signal.alarm(0)
signal.signal(signal.SIGALRM, self.command_timeout) signal.signal(signal.SIGALRM, self.command_timeout)
while True: while True:
@ -159,6 +160,10 @@ class ConnectionProcess(object):
send_data(s, to_bytes(resp)) send_data(s, to_bytes(resp))
except Exception as exc:
error = self.srv.make_error(data, to_text(exc))
send_data(s, to_bytes(error))
finally:
s.close() s.close()
except Exception as e: except Exception as e:

@ -72,6 +72,24 @@ class JsonRpcServer(object):
return response return response
def make_error(self, request, error_msg):
"""Produce a correct error payload based on request
This is here to allow a caller to produce an error response with the
correct format and response id outside of handle_request, say if an
error has occurred before handle_request could be called.
"""
request = json.loads(to_text(request, errors='surrogate_then_replace'))
setattr(self, '_identifier', request.get('id'))
error = self.internal_error(data=to_text(error_msg))
response = json.dumps(error)
delattr(self, '_identifier')
return response
def register(self, obj): def register(self, obj):
self._objects.add(obj) self._objects.add(obj)

Loading…
Cancel
Save