From 4147d0360732d309ac7d588040477da666f82afe Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Thu, 15 Jul 2021 21:56:32 +0200 Subject: [PATCH] Fix error handling. Well, actually ... implement it :) --- geoip-lookup.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/geoip-lookup.py b/geoip-lookup.py index ea7a270..3bf3474 100755 --- a/geoip-lookup.py +++ b/geoip-lookup.py @@ -82,9 +82,6 @@ def get_details(ipaddress, dbfile): reader = geoip2.database.Reader(dbfile) dbtype = reader.metadata().database_type result = None - country = None - continent = None - network = None if dbtype == 'GeoLite2-City' or dbtype == 'GeoIP2-City': result = reader.city(ipaddress) elif dbtype == 'GeoLite2-Country' or dbtype == 'GeoIP2-Country': @@ -98,6 +95,14 @@ def get_details(ipaddress, dbfile): continent = result.continent.code network = result.traits.network return "%s,%s,%s" % (country, continent, network) + except FileNotFoundError as e: + raise LookupException(e.args) + except maxminddb.errors.InvalidDatabaseError as e: + raise LookupException(e.args) + except geoip2.errors.AddressNotFoundError as e: + raise LookupException(e.args) + except ValueError as e: + raise LookupException(e.args) finally: if reader: reader.close()