Skip to content

Commit c311e81

Browse files
authored
Give more detail when we cannot process a non-JSON streamed line (#1186) (#1258)
Pack the line information into job_explanation for technical reasons Limit line length in these error messages to print to 1000 characters Update tests to check for more error reporting
1 parent 0393c39 commit c311e81

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

ansible_runner/streaming.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,13 @@ def run(self):
333333
try:
334334
line = self._input.readline()
335335
data = json.loads(line)
336-
except (json.decoder.JSONDecodeError, IOError):
337-
self.status_callback({'status': 'error', 'job_explanation': 'Failed to JSON parse a line from worker stream.'})
336+
except (json.decoder.JSONDecodeError, IOError) as exc:
337+
self.status_callback({
338+
'status': 'error',
339+
'job_explanation': (
340+
f'Failed to JSON parse a line from worker stream. Error: {exc} Line with invalid JSON data: {line[:1000]}'
341+
)
342+
})
338343
break
339344

340345
if 'status' in data:

test/integration/test_transmit_worker_process.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,15 @@ def test_garbage_private_dir_worker(tmp_path):
459459
_output=outgoing_buffer,
460460
private_data_dir=worker_dir,
461461
)
462-
sent = outgoing_buffer.getvalue()
463-
assert b'"status": "error"' in sent
462+
outgoing_buffer.seek(0)
463+
sent = outgoing_buffer.readline()
464+
data = json.loads(sent)
465+
assert data['status'] == 'error'
466+
assert data['job_explanation'] == 'Failed to extract private data directory on worker.'
467+
assert data['result_traceback']
464468

465469

466-
def test_unparsable_private_dir_worker(tmp_path):
470+
def test_unparsable_line_worker(tmp_path):
467471
worker_dir = tmp_path / 'for_worker'
468472
worker_dir.mkdir()
469473
incoming_buffer = io.BytesIO(b'')
@@ -476,18 +480,27 @@ def test_unparsable_private_dir_worker(tmp_path):
476480
_output=outgoing_buffer,
477481
private_data_dir=worker_dir,
478482
)
479-
sent = outgoing_buffer.getvalue()
480-
assert b'"status": "error"' in sent
483+
outgoing_buffer.seek(0)
484+
sent = outgoing_buffer.readline()
485+
data = json.loads(sent)
486+
assert data['status'] == 'error'
487+
assert data['job_explanation'] == 'Failed to JSON parse a line from transmit stream.'
481488

482489

483-
def test_unparsable_private_dir_processor(tmp_path):
490+
def test_unparsable_really_big_line_processor(tmp_path):
484491
process_dir = tmp_path / 'for_process'
485492
process_dir.mkdir()
486-
incoming_buffer = io.BytesIO(b'')
493+
incoming_buffer = io.BytesIO(bytes(f'not-json-data with extra garbage:{"f"*10000}', encoding='utf-8'))
494+
495+
def status_receiver(status_data, runner_config):
496+
assert status_data['status'] == 'error'
497+
assert 'Failed to JSON parse a line from worker stream.' in status_data['job_explanation']
498+
assert 'not-json-data with extra garbage:ffffffffff' in status_data['job_explanation']
499+
assert len(status_data['job_explanation']) < 2000
487500

488-
processor = run(
501+
run(
489502
streamer='process',
490503
_input=incoming_buffer,
491504
private_data_dir=process_dir,
505+
status_handler=status_receiver
492506
)
493-
assert processor.status == 'error'

0 commit comments

Comments
 (0)