From 8e9a3e8fc8e5de762455ca7472c81e5ad15f8591 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 30 Dec 2019 15:11:07 +0100 Subject: [PATCH] Capture FtBaseException at the outermost level --- freqtrade/exceptions.py | 28 +++++++++++++++++----------- freqtrade/main.py | 4 ++-- tests/test_main.py | 4 ++-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/freqtrade/exceptions.py b/freqtrade/exceptions.py index 80d45dd86..2f05ddb57 100644 --- a/freqtrade/exceptions.py +++ b/freqtrade/exceptions.py @@ -1,21 +1,27 @@ -class DependencyException(Exception): +class FreqtradeException(Exception): + """ + Freqtrade base exception. Handled at the outermost level. + All other exception types are subclasses of this exception type. + """ + + +class OperationalException(FreqtradeException): + """ + Requires manual intervention and will stop the bot. + Most of the time, this is caused by an invalid Configuration. + """ + + +class DependencyException(FreqtradeException): """ Indicates that an assumed dependency is not met. This could happen when there is currently not enough money on the account. """ -class OperationalException(Exception): - """ - Requires manual intervention and will usually stop the bot. - This happens when an exchange returns an unexpected error during runtime - or given configuration is invalid. - """ - - -class InvalidOrderException(Exception): +class InvalidOrderException(FreqtradeException): """ This is returned when the order is not valid. Example: If stoploss on exchange order is hit, then trying to cancel the order @@ -23,7 +29,7 @@ class InvalidOrderException(Exception): """ -class TemporaryError(Exception): +class TemporaryError(FreqtradeException): """ Temporary network or exchange related error. This could happen when an exchange is congested, unavailable, or the user diff --git a/freqtrade/main.py b/freqtrade/main.py index 62d2fc05d..811e29864 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -4,6 +4,7 @@ Main Freqtrade bot script. Read the documentation to know what cli arguments you need. """ +from freqtrade.exceptions import FreqtradeException, OperationalException import sys # check min. python version if sys.version_info < (3, 6): @@ -14,7 +15,6 @@ import logging from typing import Any, List from freqtrade.configuration import Arguments -from freqtrade.exceptions import OperationalException logger = logging.getLogger('freqtrade') @@ -50,7 +50,7 @@ def main(sysargv: List[str] = None) -> None: except KeyboardInterrupt: logger.info('SIGINT received, aborting ...') return_code = 0 - except OperationalException as e: + except FreqtradeException as e: logger.error(str(e)) return_code = 2 except Exception: diff --git a/tests/test_main.py b/tests/test_main.py index 83be01999..76b1bf658 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock, PropertyMock import pytest from freqtrade.configuration import Arguments -from freqtrade.exceptions import OperationalException +from freqtrade.exceptions import OperationalException, FreqtradeException from freqtrade.freqtradebot import FreqtradeBot from freqtrade.main import main from freqtrade.state import State @@ -96,7 +96,7 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None: mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) mocker.patch( 'freqtrade.worker.Worker._worker', - MagicMock(side_effect=OperationalException('Oh snap!')) + MagicMock(side_effect=FreqtradeException('Oh snap!')) ) patched_configuration_load_config_file(mocker, default_conf) mocker.patch('freqtrade.wallets.Wallets.update', MagicMock())