Extract exception handling to decorator

This commit is contained in:
Matthias 2019-04-09 06:59:07 +02:00
parent 01c93a2ee3
commit ae8660fe06
1 changed files with 45 additions and 40 deletions

View File

@ -37,6 +37,18 @@ class ApiServer(RPC):
This class starts a none blocking thread the api server runs within This class starts a none blocking thread the api server runs within
""" """
def safe_rpc(func):
def func_wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except RPCException as e:
logger.exception("API Error calling %s: %s", func.__name__, e)
return self.rest_error(f"Error querying {func.__name__}: {e}")
return func_wrapper
def __init__(self, freqtrade) -> None: def __init__(self, freqtrade) -> None:
""" """
Init the api server, and init the super class RPC Init the api server, and init the super class RPC
@ -103,11 +115,11 @@ class ApiServer(RPC):
app.add_url_rule('/profit', 'profit', view_func=self._profit, methods=['GET']) app.add_url_rule('/profit', 'profit', view_func=self._profit, methods=['GET'])
app.add_url_rule('/status', 'status', view_func=self._status, methods=['GET']) app.add_url_rule('/status', 'status', view_func=self._status, methods=['GET'])
app.add_url_rule('/balance', 'balance', view_func=self._balance, methods=['GET']) app.add_url_rule('/balance', 'balance', view_func=self._balance, methods=['GET'])
app.add_url_rule('/whitelist', 'whitelist', view_func=self._whitelist, methods=['GET'])
# TODO: Implement the following # TODO: Implement the following
# performance # performance
# forcebuy # forcebuy
# forcesell # forcesell
# whitelist
# blacklist # blacklist
# edge # edge
# help (?) # help (?)
@ -207,38 +219,34 @@ class ApiServer(RPC):
msg = self._rpc_reload_conf() msg = self._rpc_reload_conf()
return self.rest_dump(msg) return self.rest_dump(msg)
@safe_rpc
def _count(self): def _count(self):
""" """
Handler for /count. Handler for /count.
Returns the number of trades running Returns the number of trades running
""" """
try: msg = self._rpc_count()
msg = self._rpc_count() return self.rest_dump(msg)
return self.rest_dump(msg)
except RPCException as e:
return self.rest_error(str(e))
@safe_rpc
def _daily(self): def _daily(self):
""" """
Returns the last X days trading stats summary. Returns the last X days trading stats summary.
:return: stats :return: stats
""" """
try: timescale = request.args.get('timescale')
timescale = request.args.get('timescale') logger.info("LocalRPC - Daily Command Called")
logger.info("LocalRPC - Daily Command Called") timescale = int(timescale)
timescale = int(timescale)
stats = self._rpc_daily_profit(timescale, stats = self._rpc_daily_profit(timescale,
self._config['stake_currency'], self._config['stake_currency'],
self._config['fiat_display_currency'] self._config['fiat_display_currency']
) )
return self.rest_dump(stats) return self.rest_dump(stats)
except RPCException as e:
logger.exception("API Error querying daily: %s", e)
return self.rest_error(f"Error querying daily {e}")
@safe_rpc
def _profit(self): def _profit(self):
""" """
Handler for /profit. Handler for /profit.
@ -246,42 +254,39 @@ class ApiServer(RPC):
Returns a cumulative profit statistics Returns a cumulative profit statistics
:return: stats :return: stats
""" """
try: logger.info("LocalRPC - Profit Command Called")
logger.info("LocalRPC - Profit Command Called")
stats = self._rpc_trade_statistics(self._config['stake_currency'], stats = self._rpc_trade_statistics(self._config['stake_currency'],
self._config['fiat_display_currency'] self._config['fiat_display_currency']
) )
return self.rest_dump(stats) return self.rest_dump(stats)
except RPCException as e:
logger.exception("API Error calling profit: %s", e)
return self.rest_error("Error querying closed trades - maybe there are none")
@safe_rpc
def _status(self): def _status(self):
""" """
Handler for /status table. Handler for /status table.
Returns the current status of the trades in json format Returns the current status of the trades in json format
""" """
try: results = self._rpc_trade_status()
results = self._rpc_trade_status() return self.rest_dump(results)
return self.rest_dump(results)
except RPCException as e:
logger.exception("API Error calling status: %s", e)
return self.rest_error("Error querying open trades - maybe there are none.")
@safe_rpc
def _balance(self): def _balance(self):
""" """
Handler for /balance table. Handler for /balance table.
Returns the current status of the trades in json format Returns the current status of the trades in json format
""" """
try: results = self._rpc_balance(self._config.get('fiat_display_currency', ''))
results = self._rpc_balance(self._config.get('fiat_display_currency', '')) return self.rest_dump(results)
return self.rest_dump(results)
except RPCException as e: @safe_rpc
logger.exception("API Error calling status table: %s", e) def _whitelist(self):
return self.rest_error(f"{e}") """
Handler for /whitelist table.
"""
results = self._rpc_whitelist()
return self.rest_dump(results)