Skip to content

Commit f298b4f

Browse files
committed
Improve exception handling
Adds a generic error handler for unsupported databases, and makes the error messages for ipv4 and ipv6 exceptions more verbose and less stilted. Response to appliedsec#68.
1 parent c1259b4 commit f298b4f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pygeoip/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,13 @@ def id_by_addr(self, addr):
408408
409409
:arg addr: IPv4 or IPv6 address (eg. 203.0.113.30)
410410
"""
411+
if self._databaseType in (const.PROXY_EDITION, const.NETSPEED_EDITION_REV1, const.NETSPEED_EDITION_REV1_V6):
412+
raise GeoIPError('Invalid database type; this database is not supported')
411413
ipv = 6 if addr.find(':') >= 0 else 4
412414
if ipv == 4 and self._databaseType not in (const.COUNTRY_EDITION, const.NETSPEED_EDITION):
413-
raise GeoIPError('Invalid database type; expected IPv6 address')
415+
raise GeoIPError('Invalid database type; this database supports IPv6 addresses, not IPv4')
414416
if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
415-
raise GeoIPError('Invalid database type; expected IPv4 address')
417+
raise GeoIPError('Invalid database type; this database supports IPv4 addresses, not IPv6')
416418

417419
ipnum = util.ip2long(addr)
418420
return self._seek_country(ipnum) - const.COUNTRY_BEGIN

0 commit comments

Comments
 (0)