continue-custom/server/continuedev/libs/util/create_async_task.py
Nate Sesti 279593a848
Strong typing (#533)
* refactor: ♻️ get rid of continuedev.src.continuedev structure

* refactor: ♻️ switching back to server folder

* feat: ✨ make config.py imports shorter

* feat: 🔖 publish as pre-release vscode extension

* refactor: ♻️ refactor and add more completion params to ui

* build: 🏗️ download from preview S3
2023-10-04 23:47:59 -07:00

38 lines
1.1 KiB
Python

import asyncio
import traceback
from typing import Callable, Coroutine, Optional
import nest_asyncio
from .logging import logger
from .telemetry import posthog_logger
nest_asyncio.apply()
def create_async_task(
coro: Coroutine, on_error: Optional[Callable[[Exception], Coroutine]] = None
):
"""asyncio.create_task and log errors by adding a callback"""
task = asyncio.create_task(coro)
def callback(future: asyncio.Future):
try:
future.result()
except Exception as e:
formatted_tb = "\n".join(traceback.format_exception(e))
logger.critical(f"Exception caught from async task: {formatted_tb}")
posthog_logger.capture_event(
"async_task_error",
{
"error_title": e.__str__() or e.__repr__(),
"error_message": "\n".join(traceback.format_exception(e)),
},
)
# Log the error to the GUI
if on_error is not None:
asyncio.create_task(on_error(e))
task.add_done_callback(callback)
return task