Merge pull request #3269 from freqtrade/improve_exceptionlogging

[minor] Improve exception handling on critical errors
This commit is contained in:
hroff-1902 2020-05-08 15:39:54 +03:00 committed by GitHub
commit 18ec65e05f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 6 deletions

View File

@ -18,6 +18,9 @@ def start_trading(args: Dict[str, Any]) -> int:
try: try:
worker = Worker(args) worker = Worker(args)
worker.run() worker.run()
except Exception as e:
logger.error(str(e))
logger.exception("Fatal exception!")
except KeyboardInterrupt: except KeyboardInterrupt:
logger.info('SIGINT received, aborting ...') logger.info('SIGINT received, aborting ...')
finally: finally:

View File

@ -32,7 +32,7 @@ def test_setup_utils_configuration():
assert config['exchange']['secret'] == '' assert config['exchange']['secret'] == ''
def test_start_trading_fail(mocker): def test_start_trading_fail(mocker, caplog):
mocker.patch("freqtrade.worker.Worker.run", MagicMock(side_effect=OperationalException)) mocker.patch("freqtrade.worker.Worker.run", MagicMock(side_effect=OperationalException))
@ -43,16 +43,15 @@ def test_start_trading_fail(mocker):
'trade', 'trade',
'-c', 'config.json.example' '-c', 'config.json.example'
] ]
with pytest.raises(OperationalException): start_trading(get_args(args))
start_trading(get_args(args))
assert exitmock.call_count == 1 assert exitmock.call_count == 1
exitmock.reset_mock() exitmock.reset_mock()
caplog.clear()
mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(side_effect=OperationalException)) mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(side_effect=OperationalException))
with pytest.raises(OperationalException): start_trading(get_args(args))
start_trading(get_args(args))
assert exitmock.call_count == 0 assert exitmock.call_count == 0
assert log_has('Fatal exception!', caplog)
def test_list_exchanges(capsys): def test_list_exchanges(capsys):

View File

@ -115,6 +115,32 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None:
assert log_has('Oh snap!', caplog) assert log_has('Oh snap!', caplog)
def test_main_operational_exception1(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch(
'freqtrade.commands.list_commands.available_exchanges',
MagicMock(side_effect=ValueError('Oh snap!'))
)
patched_configuration_load_config_file(mocker, default_conf)
args = ['list-exchanges']
# Test Main + the KeyboardInterrupt exception
with pytest.raises(SystemExit):
main(args)
assert log_has('Fatal exception!', caplog)
assert not log_has_re(r'SIGINT.*', caplog)
mocker.patch(
'freqtrade.commands.list_commands.available_exchanges',
MagicMock(side_effect=KeyboardInterrupt)
)
with pytest.raises(SystemExit):
main(args)
assert log_has_re(r'SIGINT.*', caplog)
def test_main_reload_conf(mocker, default_conf, caplog) -> None: def test_main_reload_conf(mocker, default_conf, caplog) -> None:
patch_exchange(mocker) patch_exchange(mocker)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())