From bbf472e69b7536df83f930ff39e5ffb06d98e63f Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 27 Jul 2023 06:52:34 +0200 Subject: [PATCH] Improve errorhandling on webserver endpoint Part of https://github.com/freqtrade/frequi/issues/1387 --- freqtrade/rpc/api_server/api_v1.py | 8 +++++++- tests/rpc/test_rpc_apiserver.py | 13 +++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index c37928cbf..b42a43d94 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -269,7 +269,10 @@ def pair_history(pair: str, timeframe: str, timerange: str, strategy: str, 'timerange': timerange, 'freqaimodel': freqaimodel if freqaimodel else config.get('freqaimodel'), }) - return RPC._rpc_analysed_history_full(config, pair, timeframe, exchange) + try: + return RPC._rpc_analysed_history_full(config, pair, timeframe, exchange) + except Exception as e: + raise HTTPException(status_code=502, detail=str(e)) @router.get('/plot_config', response_model=PlotConfig, tags=['candle data']) @@ -284,7 +287,10 @@ def plot_config(strategy: Optional[str] = None, config=Depends(get_config), config1.update({ 'strategy': strategy }) + try: return PlotConfig.parse_obj(RPC._rpc_plot_config_with_strategy(config1)) + except Exception as e: + raise HTTPException(status_code=502, detail=str(e)) @router.get('/strategies', response_model=StrategyListResponse, tags=['strategy']) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index fa5492442..8c456b6db 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1476,6 +1476,12 @@ def test_api_pair_history(botclient, mocker): "&timerange=20180111-20180112") assert_response(rc, 422) + # Invalid strategy + rc = client_get(client, + f"{BASE_URI}/pair_history?pair=UNITTEST%2FBTC&timeframe={timeframe}" + "&timerange=20180111-20180112&strategy={CURRENT_TEST_STRATEGY}11") + assert_response(rc, 502) + # Working rc = client_get(client, f"{BASE_URI}/pair_history?pair=UNITTEST%2FBTC&timeframe={timeframe}" @@ -1510,8 +1516,7 @@ def test_api_pair_history(botclient, mocker): f"{BASE_URI}/pair_history?pair=UNITTEST%2FBTC&timeframe={timeframe}" f"&timerange=20200111-20200112&strategy={CURRENT_TEST_STRATEGY}") assert_response(rc, 502) - assert rc.json()['error'] == ("Error querying /api/v1/pair_history: " - "No data for UNITTEST/BTC, 5m in 20200111-20200112 found.") + assert rc.json()['detail'] == ("No data for UNITTEST/BTC, 5m in 20200111-20200112 found.") def test_api_plot_config(botclient, mocker): @@ -1548,6 +1553,10 @@ def test_api_plot_config(botclient, mocker): assert_response(rc) assert rc.json()['subplots'] == {} + rc = client_get(client, f"{BASE_URI}/plot_config?strategy=NotAStrategy") + assert_response(rc, 502) + assert rc.json()['detail'] is not None + mocker.patch('freqtrade.rpc.api_server.api_v1.get_rpc_optional', return_value=None) rc = client_get(client, f"{BASE_URI}/plot_config")