Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions #310

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improved exceptions handling. Now exceptions contain the info about t…
…he exceptions that caused them.
KOLANICH committed Feb 16, 2021
commit 3cee2f840e2802e2c6c85645910f6eba4dcdb344
8 changes: 4 additions & 4 deletions ipwhois/asn.py
Original file line number Diff line number Diff line change
@@ -165,7 +165,7 @@ def parse_fields_dns(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

@@ -222,7 +222,7 @@ def parse_fields_verbose_dns(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

@@ -279,7 +279,7 @@ def parse_fields_whois(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

@@ -379,7 +379,7 @@ def parse_fields_http(self, response, extra_org_map=None):
except Exception as e: # pragma: no cover

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return asn_data

6 changes: 3 additions & 3 deletions ipwhois/experimental.py
Original file line number Diff line number Diff line change
@@ -103,11 +103,11 @@ def get_bulk_asn_whois(addresses=None, retry_count=3, timeout=120):

else:

raise ASNLookupError('ASN bulk lookup failed.')
raise ASNLookupError('ASN bulk lookup failed.') from e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise ASNLookupError('ASN bulk lookup failed.')
raise ASNLookupError('ASN bulk lookup failed.') from e


def bulk_lookup_rdap(addresses=None, inc_raw=False, retry_count=3, depth=0,
59 changes: 28 additions & 31 deletions ipwhois/net.py
Original file line number Diff line number Diff line change
@@ -229,13 +229,13 @@ def get_asn_dns(self):
raise ASNLookupError(
'ASN lookup failed (DNS {0}) for {1}.'.format(
e.__class__.__name__, self.address_str)
)
) from e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(self.address_str)
)
) from e

def get_asn_verbose_dns(self, asn=None):
"""
@@ -271,13 +271,13 @@ def get_asn_verbose_dns(self, asn=None):
raise ASNLookupError(
'ASN lookup failed (DNS {0}) for {1}.'.format(
e.__class__.__name__, asn)
)
) from e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(asn)
)
) from e

def get_asn_whois(self, retry_count=3):
"""
@@ -337,13 +337,13 @@ def get_asn_whois(self, retry_count=3):

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(self.address_str)
)
) from e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(self.address_str)
)
) from e

def get_asn_http(self, retry_count=3):
"""
@@ -391,9 +391,9 @@ def get_asn_http(self, retry_count=3):

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(self.address_str)
)
) from e

except:
except BaseException as e:

raise ASNLookupError(
'ASN lookup failed for {0}.'.format(self.address_str)
@@ -505,11 +505,11 @@ def get_asn_origin_whois(self, asn_registry='radb', asn=None,

raise

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise WhoisLookupError(
'ASN origin WHOIS lookup failed for {0}.'.format(asn)
)
) from e

def get_whois(self, asn_registry='arin', retry_count=3, server=None,
port=43, extra_blacklist=None):
@@ -624,7 +624,7 @@ def get_whois(self, asn_registry='arin', retry_count=3, server=None,

raise WhoisLookupError(
'WHOIS lookup failed for {0}.'.format(self.address_str)
)
) from e

except WhoisRateLimitError: # pragma: no cover

@@ -634,7 +634,7 @@ def get_whois(self, asn_registry='arin', retry_count=3, server=None,

raise

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise WhoisLookupError(
'WHOIS lookup failed for {0}.'.format(self.address_str)
@@ -673,12 +673,9 @@ def get_http_json(self, url=None, retry_count=3, rate_limit_timeout=120,
# Create the connection for the whois query.
log.debug('HTTP query for {0} at {1}'.format(
self.address_str, url))
conn = Request(url, headers=headers)
data = self.opener.open(conn, timeout=self.timeout)
try:
d = json.loads(data.readall().decode('utf-8', 'ignore'))
except AttributeError: # pragma: no cover
d = json.loads(data.read().decode('utf-8', 'ignore'))
r = self.http_client.get(url, headers=headers)
r.raise_for_status()
d = r.json

try:
# Tests written but commented out. I do not want to send a
@@ -730,12 +727,12 @@ def get_http_json(self, url=None, retry_count=3, rate_limit_timeout=120,
raise HTTPRateLimitError(
'HTTP lookup failed for {0}. Rate limit '
'exceeded, wait and try again (possibly a '
'temporary block).'.format(url))
'temporary block).'.format(url)) from e

else:

raise HTTPLookupError('HTTP lookup failed for {0} with error '
'code {1}.'.format(url, str(e.code)))
'code {1}.'.format(url, str(e.code))) from e

except (URLError, socket.timeout, socket.error) as e:

@@ -753,15 +750,15 @@ def get_http_json(self, url=None, retry_count=3, rate_limit_timeout=120,
else:

raise HTTPLookupError('HTTP lookup failed for {0}.'.format(
url))
url)) from e

except (HTTPLookupError, HTTPRateLimitError) as e: # pragma: no cover

raise e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise HTTPLookupError('HTTP lookup failed for {0}.'.format(url))
raise HTTPLookupError('HTTP lookup failed for {0}.'.format(url)) from e

def get_host(self, retry_count=3):
"""
@@ -819,11 +816,11 @@ def get_host(self, retry_count=3):
'Host lookup failed for {0}.'.format(self.address_str)
)

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise HostLookupError(
'Host lookup failed for {0}.'.format(self.address_str)
)
) from e

def get_http_raw(self, url=None, retry_count=3, headers=None,
request_type='GET', form_data=None):
@@ -896,12 +893,12 @@ def get_http_raw(self, url=None, retry_count=3, headers=None,
else:

raise HTTPLookupError('HTTP lookup failed for {0}.'.format(
url))
url)) from e

except HTTPLookupError as e: # pragma: no cover

raise e

except Exception: # pragma: no cover
except BaseException as e: # pragma: no cover

raise HTTPLookupError('HTTP lookup failed for {0}.'.format(url))
raise HTTPLookupError('HTTP lookup failed for {0}.'.format(url)) from e
20 changes: 10 additions & 10 deletions ipwhois/rdap.py
Original file line number Diff line number Diff line change
@@ -476,9 +476,9 @@ def __init__(self, json_result):

_RDAPCommon.__init__(self, json_result)

except ValueError:
except ValueError as e:

raise InvalidNetworkObject('JSON result must be a dict.')
raise InvalidNetworkObject('JSON result must be a dict.') from e

self.vars.update({
'start_address': None,
@@ -500,12 +500,12 @@ def parse(self):

self.vars['handle'] = self.json['handle'].strip()

except (KeyError, ValueError):
except (KeyError, ValueError) as e:

log.debug('Handle missing, json_output: {0}'.format(json.dumps(
self.json)))
raise InvalidNetworkObject('Handle is missing for RDAP network '
'object')
'object') from e

try:

@@ -529,12 +529,12 @@ def parse(self):
self.vars['start_address'] = self.json['startAddress'].strip()
self.vars['end_address'] = self.json['endAddress'].strip()

except (KeyError, ValueError, TypeError):
except (KeyError, ValueError, TypeError) as e:

log.debug('IP address data incomplete. Data parsed prior to '
'exception: {0}'.format(json.dumps(self.vars)))
raise InvalidNetworkObject('IP address data is missing for RDAP '
'network object.')
'network object.') from e

try:

@@ -587,9 +587,9 @@ def __init__(self, json_result):

_RDAPCommon.__init__(self, json_result)

except ValueError:
except ValueError as e:

raise InvalidEntityObject('JSON result must be a dict.')
raise InvalidEntityObject('JSON result must be a dict.') from e

self.vars.update({
'roles': None,
@@ -607,9 +607,9 @@ def parse(self):

self.vars['handle'] = self.json['handle'].strip()

except (KeyError, ValueError, TypeError):
except (KeyError, ValueError, TypeError) as e:

raise InvalidEntityObject('Handle is missing for RDAP entity')
raise InvalidEntityObject('Handle is missing for RDAP entity') from e

for v in ['roles', 'country']:

6 changes: 0 additions & 6 deletions ipwhois/tests/online/test_asn.py
Original file line number Diff line number Diff line change
@@ -24,8 +24,6 @@ def test_lookup(self):
self.assertIsInstance(ipasn.lookup(inc_raw=True), dict)
except ASNRegistryError:
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

@@ -80,10 +78,6 @@ def test_lookup(self):

raise e

except Exception as e:

self.fail('Unexpected exception raised: {0}'.format(e))

net = Net(address='74.125.225.229', timeout=0)
asnorigin = ASNOrigin(net)
self.assertRaises(ASNOriginLookupError, asnorigin.lookup, **dict(
4 changes: 0 additions & 4 deletions ipwhois/tests/online/test_experimental.py
Original file line number Diff line number Diff line change
@@ -32,8 +32,6 @@ def test_get_bulk_asn_whois(self):
self.assertIsInstance(get_bulk_asn_whois(addresses=ips), str)
except ASNLookupError:
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

@@ -86,7 +84,5 @@ def test_bulk_lookup_rdap(self):

except ASNLookupError:
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))
10 changes: 0 additions & 10 deletions ipwhois/tests/online/test_ipwhois.py
Original file line number Diff line number Diff line change
@@ -51,8 +51,6 @@ def test_lookup_whois(self):
except (ASNLookupError, ASNRegistryError, WhoisLookupError,
HTTPLookupError):
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

@@ -70,8 +68,6 @@ def test_lookup_whois(self):
inc_raw=True), dict)
except (ASNLookupError, ASNRegistryError, WhoisLookupError):
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

@@ -86,8 +82,6 @@ def test_lookup_whois(self):
extra_blacklist=['rwhois.cogentco.com']), dict)
except (ASNLookupError, ASNRegistryError, WhoisLookupError):
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

@@ -164,10 +158,6 @@ def test_lookup_rdap(self):
except (ASNLookupError, ASNRegistryError, WhoisLookupError,
HTTPLookupError):
pass
except AssertionError as e:
raise e
except Exception as e:
self.fail('Unexpected exception raised: {0}'.format(e))

handler = ProxyHandler({'http': 'http://0.0.0.0:80/'})
opener = build_opener(handler)
2 changes: 1 addition & 1 deletion ipwhois/tests/stress/test_net.py
Original file line number Diff line number Diff line change
@@ -39,5 +39,5 @@ def test_get_http_json(self):
raise Exception('HTTPLookupError has been raised 5 times. '
'Likely cause is socket connection '
'timeouts. Quitting test to avoid an '
'endless loop.')
'endless loop.') from e
continue
4 changes: 0 additions & 4 deletions ipwhois/tests/test_nir.py
Original file line number Diff line number Diff line change
@@ -54,10 +54,6 @@ def test_lookup(self):

raise e

except Exception as e:

self.fail('Unexpected exception raised: {0}'.format(e))

self.assertRaises(NetError, NIRWhois, 'a')
self.assertRaises(KeyError, obj.lookup)
self.assertRaises(KeyError, obj.lookup, **dict(nir='a'))