Handle tasks exception

This commit is contained in:
arabcoders 2025-06-24 16:26:56 +03:00
parent a9cb816050
commit 7e29e7b48c

View file

@ -223,19 +223,19 @@ class Tasks(metaclass=Singleton):
if not task.get("name"): if not task.get("name"):
msg = "No name found." msg = "No name found."
raise ValueError(msg) raise ValueError(msg)
else:
task["name"] = task["name"].strip() task["name"] = task["name"].strip()
if not task.get("url"): if not task.get("url"):
msg = "No URL found." msg = "No URL found."
raise ValueError(msg) raise ValueError(msg)
else:
task["url"] = task["url"].strip() task["url"] = task["url"].strip()
try: try:
validate_url(task["url"], allow_internal=True) validate_url(task["url"], allow_internal=True)
except ValueError as e: except ValueError as e:
msg = f"Invalid URL format. '{e!s}'." msg = f"Invalid URL format. '{e!s}'."
raise ValueError(msg) from e raise ValueError(msg) from e
if task.get("timer"): if task.get("timer"):
try: try:
@ -380,10 +380,19 @@ class HandleTask:
if handler is None: if handler is None:
continue continue
asyncio.create_task(self.dispatch(task, handler=handler), name=f"task-{task.id}") coro = self.dispatch(task, handler=handler)
t = asyncio.create_task(coro, name=f"task-{task.id}")
t.add_done_callback(lambda fut, t=task: self._handle_exception(fut, t))
except Exception as e: except Exception as e:
LOG.error(f"Failed to handle task '{task.name}'. '{e!s}'.") LOG.error(f"Failed to handle task '{task.name}'. '{e!s}'.")
def _handle_exception(self, fut: asyncio.Task, task: Task) -> None:
if fut.cancelled():
return
if exc := fut.exception():
LOG.error(f"Exception while handling task '{task.name}': {exc}")
def _find_handler(self, task: Task) -> type | None: def _find_handler(self, task: Task) -> type | None:
for cls in self._handlers: for cls in self._handlers:
try: try: