Improving the get_address() function

With the get_address() method, we've continued adding logging messages to our script. This time, when catching URLError, we stored the Exception object as e to extract additional information from it for debugging:

059 def get_address(address):
060 """
061 The get_address function uses the blockchain.info Data API
062 to pull pull down account information and transactions for
063 address of interest
064 :param address: The Bitcoin Address to lookup
065 :return: The response of the url request
066 """

For URLError, we'll want to log the code, headers, and reason attributes. These attributes contain information, such as the HTML error code—for example, 404 for a web page that isn't found—and a description of the reason for the error code. We'll store this data to preserve the context surrounding the error:

067     url = 'https://blockchain.info/address/{}?format=json'
068 formatted_url = url.format(address)
069 try:
070 return urllib.request.urlopen(formatted_url)
071 except urllib.error.URLError as e:
072 logging.error('URL Error for {}'.format(formatted_url))
073 if hasattr(e, 'code') and hasattr(e, 'headers'):
074 logging.debug('{}: {}'.format(e.code, e.reason))
075 logging.debug('{}'.format(e.headers))
076 print('Received URL Error for {}'.format(formatted_url))
077 logging.info('Program exiting...')
078 sys.exit(1)