corpetty/py-etherscan-api

Adding In Exceptions

taylorjdawson opened this issue · 1 comments

def connect(self):
# TODO: deal with "unknown exception" error
try:
req = self.http.get(self.url)
except requests.exceptions.ConnectionError:
print("Connection refused")
exit()
if req.status_code == 200:
# Check for empty response
if req.text:
if req.json()['status'] == '1':
return req.json()
else:
print(req.json()['message'])
exit()
else:
print("Invalid Request")
exit()
else:
print("Problem with connection, status code: ", req.status_code)
exit()

I think it would be a good idea to throw exceptions instead of printing error messages and then exiting. For instance, if the user was to loop over a list of addresses to check the balance but there were some invalid addresses in the list, as of now, the api would break the loop. Throwing exceptions will enable the user to catch and handle the exceptions themselves.

I second this, a library should not make exit in the middle of the code, but let the application using the library handling it.
Here's a pull request that solves it.
#23