From d393fd33ed87b2bc0138cd723d14c633fac089f4 Mon Sep 17 00:00:00 2001 From: nerfZael Date: Mon, 10 Jun 2024 20:48:06 +0200 Subject: [PATCH] error handling; default autotx params and setting port in cli --- autotx/cli.py | 2 +- autotx/server.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/autotx/cli.py b/autotx/cli.py index 8da06e1..1a4a2f7 100644 --- a/autotx/cli.py +++ b/autotx/cli.py @@ -83,7 +83,7 @@ def serve(verbose: bool, logs: str | None, max_rounds: int | None, cache: bool, print_autotx_info() setup_server(verbose, logs, max_rounds, cache, dev, check_valid_safe=True) - uvicorn.run("autotx.server:app", host="localhost", port=8000, workers=1) + uvicorn.run("autotx.server:app", host="localhost", port=port if port else 8000, workers=1) @main.command() @click.option("-n", "--name", type=str, help="Name of the application to create") diff --git a/autotx/server.py b/autotx/server.py index b2fc2d1..5ef3ce2 100644 --- a/autotx/server.py +++ b/autotx/server.py @@ -35,7 +35,7 @@ def __init__(self, self.max_rounds = max_rounds self.is_dev = is_dev -autotx_params: AutoTxParams | None = None +autotx_params: AutoTxParams = AutoTxParams(verbose=False, logs=None, cache=False, max_rounds=None, is_dev=False) app_router = APIRouter() @@ -107,7 +107,15 @@ def on_notify_user(message: str) -> None: ) async def run_task() -> None: - await autotx.a_run(prompt, non_interactive=True) + try: + await autotx.a_run(prompt, non_interactive=True) + except Exception as e: + task = tasks.get(task_id) + if task is None: + raise Exception("Task not found: " + task_id) + + task.messages.append(str(e)) + tasks.update(task) tasks.stop(task_id) background_tasks.add_task(run_task) @@ -180,6 +188,9 @@ def send_transactions(task_id: str, model: models.SendTransactionsParams, author global autotx_params if autotx_params is None: raise HTTPException(status_code=500, detail="AutoTx not started") + + if task.transactions is None or len(task.transactions) == 0: + raise HTTPException(status_code=400, detail="No transactions to send") if autotx_params.is_dev: print("Dev mode: skipping transaction submission")