From 4c52109fa34bfcdf86dd7ebca352c013a2b499d2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 24 May 2023 20:37:23 +0200 Subject: [PATCH] Handle pairlist evaluation errors gracefully --- freqtrade/rpc/api_server/api_v1.py | 12 ++++++++++-- freqtrade/rpc/api_server/webserver_bgwork.py | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 54c3a1084..411b622b4 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -4,6 +4,7 @@ from typing import List, Optional from fastapi import APIRouter, BackgroundTasks, Depends, Query from fastapi.exceptions import HTTPException +from sympy import O from freqtrade import __version__ from freqtrade.constants import Config @@ -343,7 +344,9 @@ def __run_pairlist(config_loc: Config): 'length': len(pairlists.whitelist), 'whitelist': pairlists.whitelist } - + except (OperationalException, Exception) as e: + logger.exception(e) + ApiBG.pairlist_error = str(e) finally: ApiBG.pairlist_running = False @@ -360,7 +363,8 @@ def pairlists_evaluate(payload: PairListsPayload, background_tasks: BackgroundTa # TODO: overwrite blacklist? make it optional and fall back to the one in config? # Outcome depends on the UI approach. config_loc['exchange']['pair_blacklist'] = payload.blacklist - + ApiBG.pairlist_error = None + ApiBG.pairlist_result = {} background_tasks.add_task(__run_pairlist, config_loc) ApiBG.pairlist_running = True return { @@ -370,6 +374,10 @@ def pairlists_evaluate(payload: PairListsPayload, background_tasks: BackgroundTa @router.get('/pairlists/evaluate', response_model=WhitelistResponse, tags=['pairlists']) def pairlists_evaluate_get(): + if ApiBG.pairlist_error: + raise HTTPException(status_code=500, + detail='Pairlist evaluation failed: ' + ApiBG.pairlist_error) + if ApiBG.pairlist_running: raise HTTPException(status_code=202, detail='Pairlist evaluation is currently running.') diff --git a/freqtrade/rpc/api_server/webserver_bgwork.py b/freqtrade/rpc/api_server/webserver_bgwork.py index 3ab36ced1..1d2565265 100644 --- a/freqtrade/rpc/api_server/webserver_bgwork.py +++ b/freqtrade/rpc/api_server/webserver_bgwork.py @@ -1,5 +1,5 @@ -from typing import Any, Dict +from typing import Any, Dict, Optional class ApiBG(): @@ -15,5 +15,6 @@ class ApiBG(): # Exchange - only available in webserver mode. exchange = None # Pairlist evaluate things + pairlist_error: Optional[str] = None pairlist_running: bool = False pairlist_result: Dict[str, Any] = {}