From 0744c144e037dd5f5649433c7a399d5b362c9d6c Mon Sep 17 00:00:00 2001 From: Michael Robinson Date: Sun, 12 Feb 2017 19:06:56 +0000 Subject: [PATCH] Fixing and standarising exceptions, fixes #181 (I think, can't reproduce easily) --- lendingbot.py | 10 +++++----- modules/Configuration.py | 16 ++++++++++------ modules/Data.py | 7 ++++--- modules/Lending.py | 5 +++-- modules/MarketAnalysis.py | 5 +++-- modules/Poloniex.py | 7 +++---- modules/WebServer.py | 8 +++++--- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lendingbot.py b/lendingbot.py index 96aefa04..d30c931d 100644 --- a/lendingbot.py +++ b/lendingbot.py @@ -78,21 +78,21 @@ # allow existing the main bot loop raise except Exception as ex: - log.log_error(str(ex)) + log.log_error(ex.message) log.persistStatus() - if 'Invalid API key' in str(ex): + if 'Invalid API key' in ex.message: print "!!! Troubleshooting !!!" print "Are your API keys correct? No quotation. Just plain keys." exit(1) - elif 'Nonce must be greater' in str(ex): + elif 'Nonce must be greater' in ex.message: print "!!! Troubleshooting !!!" print "Are you reusing the API key in multiple applications? Use a unique key for every application." exit(1) - elif 'Permission denied' in str(ex): + elif 'Permission denied' in ex.message: print "!!! Troubleshooting !!!" print "Are you using IP filter on the key? Maybe your IP changed?" exit(1) - elif 'timed out' in str(ex): + elif 'timed out' in ex.message: print "Timed out, will retry in " + str(Lending.get_sleep_time()) + "sec" else: print traceback.format_exc() diff --git a/modules/Configuration.py b/modules/Configuration.py index 19eece89..482ae31a 100644 --- a/modules/Configuration.py +++ b/modules/Configuration.py @@ -24,7 +24,8 @@ def init(file_location, data=None): raw_input("Press Enter to acknowledge and exit...") exit(1) except Exception as ex: - print "Failed to automatically copy config. Please do so manually. Error: " + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("Failed to automatically copy config. Please do so manually. Error: {0}".format(ex.message)) exit(1) return config @@ -71,7 +72,8 @@ def get_coin_cfg(): maxtolend=Decimal(cur[3]), maxpercenttolend=(Decimal(cur[4])) / 100, maxtolendrate=(Decimal(cur[5])) / 100) except Exception as ex: - print "Coinconfig parsed incorrectly, please refer to the documentation. Error: " + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("Coinconfig parsed incorrectly, please refer to the documentation. Error: {0}".format(ex.message)) else: for cur in FULL_LIST: if config.has_section(cur): @@ -83,8 +85,9 @@ def get_coin_cfg(): coin_cfg[cur]['maxpercenttolend'] = (Decimal(config.get(cur, 'maxpercenttolend'))) / 100 coin_cfg[cur]['maxtolendrate'] = (Decimal(config.get(cur, 'maxtolendrate'))) / 100 except Exception as ex: - print "Coinconfig for " + cur + " parsed incorrectly, please refer to the documentation. " + \ - "Error: " + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("Coinconfig for " + cur + " parsed incorrectly, please refer to the documentation. " + "Error: {0}".format(ex.message)) # Need to raise this exception otherwise the bot will continue if you configured one cur correctly raise return coin_cfg @@ -97,8 +100,9 @@ def get_min_loan_sizes(): try: min_loan_sizes[cur] = Decimal(get(cur, 'minloansize', lower_limit=0.01)) except Exception as ex: - print "minloansize for " + cur + " parsed incorrectly, please refer to the documentation. " + \ - "Error: " + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("minloansize for " + cur + " parsed incorrectly, please refer to the documentation. " + "Error: {0}".format(ex.message)) # Bomb out if something isn't configured correctly raise return min_loan_sizes diff --git a/modules/Data.py b/modules/Data.py index 77fb1f07..56f72163 100644 --- a/modules/Data.py +++ b/modules/Data.py @@ -35,8 +35,9 @@ def get_max_duration(end_date, context): return diff_days # Order needs int if context == "status": return " - Days Remaining: " + str(diff_days) # Status needs string - except Exception as Ex: - print "ERROR: There is something wrong with your endDate option. Error: " + str(Ex) + except Exception as ex: + ex.message = ex.message if ex.message else str(ex) + print("ERROR: There is something wrong with your endDate option. Error: {0}".format(ex.message)) exit(1) @@ -134,4 +135,4 @@ def truncate(f, n): if 'e' in s or 'E' in s: return '{0:.{1}f}'.format(f, n) i, p, d = s.partition('.') - return '.'.join([i, (d+'0'*n)[:n]]) \ No newline at end of file + return '.'.join([i, (d+'0'*n)[:n]]) diff --git a/modules/Lending.py b/modules/Lending.py index 9b1377e0..356bcf43 100644 --- a/modules/Lending.py +++ b/modules/Lending.py @@ -137,8 +137,9 @@ def cancel_all(): try: msg = api.cancel_loan_offer(CUR, offer['id']) log.cancelOrders(CUR, msg) - except Exception as Ex: - log.log("Error canceling loan offer: " + str(Ex)) + except Exception as ex: + ex.message = ex.message if ex.message else str(ex) + log.log("Error canceling loan offer: {0}".format(ex.message)) else: print "Not enough " + CUR + " to lend if bot canceled open orders. Not cancelling." diff --git a/modules/MarketAnalysis.py b/modules/MarketAnalysis.py index 94a76bc0..0f862eb7 100644 --- a/modules/MarketAnalysis.py +++ b/modules/MarketAnalysis.py @@ -7,8 +7,9 @@ import numpy use_numpy = True except ImportError as ex: - print "WARN: Module Numpy not found, using manual percentile method instead. " \ - "It is recommended to install Numpy. Error: " + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("WARN: Module Numpy not found, using manual percentile method instead. " + "It is recommended to install Numpy. Error: {0}".format(ex.message)) use_numpy = False currencies_to_analyse = [] diff --git a/modules/Poloniex.py b/modules/Poloniex.py index 663fc2f7..c4f3544e 100644 --- a/modules/Poloniex.py +++ b/modules/Poloniex.py @@ -105,10 +105,9 @@ def _read_response(resp): json_ret = _read_response(ret) return post_process(json_ret) except Exception as ex: - # add command information to exception - # (this isn't compatible with python 3) - import sys - raise type(ex), type(ex)(ex.message + ' Requesting %s' % command), sys.exc_info()[2] + ex.message = ex.message if ex.message else str(ex) + ex.message = "{0} Requesting {1}".format(ex.message, command) + raise def return_ticker(self): return self.api_query("returnTicker") diff --git a/modules/WebServer.py b/modules/WebServer.py index 600fc9d7..f906d718 100644 --- a/modules/WebServer.py +++ b/modules/WebServer.py @@ -52,8 +52,9 @@ def translate_path(self, path): host1 = host print 'Started WebServer, lendingbot status available at http://' + host1 + ':' + str(port) + '/lendingbot.html' server.serve_forever() - except Exception as Ex: - print 'Failed to start WebServer' + str(Ex) + except Exception as ex: + ex.message = ex.message if ex.message else str(ex) + print('Failed to start WebServer: {0}'.format(ex.message)) def stop_web_server(): @@ -61,4 +62,5 @@ def stop_web_server(): print "Stopping WebServer" server.shutdown() except Exception as ex: - print 'Failed to stop WebServer' + str(ex) + ex.message = ex.message if ex.message else str(ex) + print("Failed to stop WebServer: {0}".format(ex.message))