JohnSnowLabs / langtest

Deliver safe & effective language models
http://langtest.org/
Apache License 2.0
488 stars 36 forks source link

Missing Error Handling in asyncio.run Method #989

Closed chakravarthik27 closed 4 months ago

chakravarthik27 commented 6 months ago

Description

The provided code snippet lacks error handling techniques within the asyncio.run method, which can lead to unhandled exceptions and potential program crashes. Specifically, if any errors occur during the execution of asynchronous tasks initiated by asyncio.run, they are not caught or handled appropriately.

Code Snippet:

async_tests = TestFactory.async_run(samples_list, model_handler, **kwargs)
temp_res = asyncio.run(async_tests)
results = []
for each in temp_res:
    if hasattr(each, "_result"):
        results.extend(each._result)
    elif isinstance(each, list):
        for i in each:
            if hasattr(i, "_result"):
                results.extend(i._result)
            else:
                results.append(i)

return results

Proposed Solution:

Implement error-catching techniques within the asyncio.run method to handle exceptions gracefully. This can prevent crashes and provide better error reporting to aid in debugging.

Suggested Changes:

  1. Surround the asyncio.run call with a try-except block to catch any exceptions raised during asynchronous execution.
  2. Provide appropriate error handling within the except block, such as logging the error or returning an error message.
  3. Ensure that the error handling mechanism does not disrupt the flow of the program and allows it to gracefully handle errors while continuing execution.

Image