Skip to content

Commit

Permalink
rework exception handling (fixes #108)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarq committed Nov 16, 2017
1 parent a0bb7a6 commit 0bc9624
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
13 changes: 7 additions & 6 deletions freqtrade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from freqtrade import __version__, exchange, persistence
from freqtrade.analyze import get_buy_signal
from freqtrade.misc import CONF_SCHEMA, State, get_state, update_state, build_arg_parser, throttle
from freqtrade.misc import CONF_SCHEMA, State, get_state, update_state, build_arg_parser, throttle, \
FreqtradeException
from freqtrade.persistence import Trade
from freqtrade.rpc import telegram

Expand Down Expand Up @@ -76,8 +77,8 @@ def _process(dynamic_whitelist: Optional[bool] = False) -> bool:
'Checked all whitelisted currencies. '
'Found no suitable entry positions for buying. Will keep looking ...'
)
except ValueError:
logger.exception('Unable to create trade')
except FreqtradeException as e:
logger.warning('Unable to create trade: %s', e)

for trade in trades:
# Get order details for actual price per unit
Expand All @@ -93,7 +94,7 @@ def _process(dynamic_whitelist: Optional[bool] = False) -> bool:
Trade.session.flush()
except (requests.exceptions.RequestException, json.JSONDecodeError) as error:
msg = 'Got {} in _process(), retrying in 30 seconds...'.format(error.__class__.__name__)
logger.exception(msg)
logger.warning(msg)
time.sleep(30)
except RuntimeError:
telegram.send_msg('*Status:* Got RuntimeError:\n```\n{traceback}```{hint}'.format(
Expand Down Expand Up @@ -206,7 +207,7 @@ def create_trade(stake_amount: float) -> Optional[Trade]:
whitelist = copy.deepcopy(_CONF['exchange']['pair_whitelist'])
# Check if stake_amount is fulfilled
if exchange.get_balance(_CONF['stake_currency']) < stake_amount:
raise ValueError(
raise FreqtradeException(
'stake amount is not fulfilled (currency={})'.format(_CONF['stake_currency'])
)

Expand All @@ -216,7 +217,7 @@ def create_trade(stake_amount: float) -> Optional[Trade]:
whitelist.remove(trade.pair)
logger.debug('Ignoring %s in pair whitelist', trade.pair)
if not whitelist:
raise ValueError('No pair in whitelist')
raise FreqtradeException('No pair in whitelist')

# Pick pair based on StochRSI buy signals
for _pair in whitelist:
Expand Down
4 changes: 4 additions & 0 deletions freqtrade/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
logger = logging.getLogger(__name__)


class FreqtradeException(BaseException):
pass


class State(enum.Enum):
RUNNING = 0
STOPPED = 1
Expand Down
6 changes: 3 additions & 3 deletions freqtrade/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from freqtrade.exchange import Exchanges
from freqtrade.main import create_trade, handle_trade, close_trade_if_fulfilled, init, \
get_target_bid, _process
from freqtrade.misc import get_state, State
from freqtrade.misc import get_state, State, FreqtradeException
from freqtrade.persistence import Trade


Expand Down Expand Up @@ -139,7 +139,7 @@ def test_create_trade_no_stake_amount(default_conf, ticker, mocker):
get_ticker=ticker,
buy=MagicMock(return_value='mocked_limit_buy'),
get_balance=MagicMock(return_value=default_conf['stake_amount'] * 0.5))
with pytest.raises(ValueError, match=r'.*stake amount.*'):
with pytest.raises(FreqtradeException, match=r'.*stake amount.*'):
create_trade(default_conf['stake_amount'])


Expand All @@ -152,7 +152,7 @@ def test_create_trade_no_pairs(default_conf, ticker, mocker):
get_ticker=ticker,
buy=MagicMock(return_value='mocked_limit_buy'))

with pytest.raises(ValueError, match=r'.*No pair in whitelist.*'):
with pytest.raises(FreqtradeException, match=r'.*No pair in whitelist.*'):
conf = copy.deepcopy(default_conf)
conf['exchange']['pair_whitelist'] = []
mocker.patch.dict('freqtrade.main._CONF', conf)
Expand Down

0 comments on commit 0bc9624

Please sign in to comment.