tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

[Query] How to propagate the underlying assertion error message from tenacity?

Open debraj-manna opened this issue 10 months ago • 2 comments

I am using tenacity 9.0.0 with Python 3.9. My code is like below

@pytest.mark.monitoring_service
def test():
    @retry(stop=stop_after_attempt(6), wait=wait_fixed(5))
    def assert_response():
        logging.info("Retrying get flow logs")
        es.indices.refresh(index='monitoring')
        response = requests.get(url, headers=headers, json=data, params=params)
        assert response.status_code == 200, f"Response status is {response.status_code} and body: {response.text}"
        response_json = response.json()
        logging.info("Response: " + str(response_json))
        assert response_json['meta']['run_id'] == 1742403068092, "run_id does not match"

    try:
        assert_response()
    except RetryError as e:
        underlying_error = e.last_attempt.exception()
        error_message = str(underlying_error)
        logging.error(f"Failed to get flow logs after retries: {underlying_error}")
        raise AssertionError(f"Failed to get flow logs after multiple retries: {error_message}") from underlying_error

I am seeing the error logs like below when retry gets exhausted.

integration-tests-1  | FAILED test_apps/monitoring_service/test_es_monitoring_dao.py::test - AssertionError: Failed to get flow logs after multiple retries: 'run_id'
integration-tests-1  | ====================== 1 failed, 130 deselected in 11.42s ======================
integration-tests-1  | Result of the pytest script: 1

How can I get the underlying log run_id does not match ?

debraj-manna avatar Mar 31 '25 16:03 debraj-manna

The same is asked in stackoverflow also.

debraj-work avatar Apr 12 '25 17:04 debraj-work

There's a reraise kwargs for retry in getting started docs, is that what you're looking for?

YeungOnion avatar Apr 15 '25 13:04 YeungOnion