Merge pull request #1535 from dbungert/no-except-on-geoip-failure
geoip: do not raise on geoip failure
This commit is contained in:
commit
c6564de0dc
|
@ -35,10 +35,6 @@ class CheckState(enum.IntEnum):
|
||||||
DONE = enum.auto()
|
DONE = enum.auto()
|
||||||
|
|
||||||
|
|
||||||
class LookupError(Exception):
|
|
||||||
""" Error to raise when retrieving the GeoIP information. """
|
|
||||||
|
|
||||||
|
|
||||||
class GeoIPStrategy(ABC):
|
class GeoIPStrategy(ABC):
|
||||||
""" Base class for strategies (e.g. HTTP or dry-run) to retrieve the GeoIP
|
""" Base class for strategies (e.g. HTTP or dry-run) to retrieve the GeoIP
|
||||||
information. """
|
information. """
|
||||||
|
@ -76,13 +72,10 @@ class HTTPGeoIPStrategy(GeoIPStrategy):
|
||||||
geoip.ubuntu.com service. """
|
geoip.ubuntu.com service. """
|
||||||
async def get_response(self) -> str:
|
async def get_response(self) -> str:
|
||||||
url = "https://geoip.ubuntu.com/lookup"
|
url = "https://geoip.ubuntu.com/lookup"
|
||||||
try:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with session.get(url) as response:
|
||||||
async with session.get(url) as response:
|
response.raise_for_status()
|
||||||
response.raise_for_status()
|
return await response.text()
|
||||||
return await response.text()
|
|
||||||
except aiohttp.ClientError as e:
|
|
||||||
raise LookupError from e
|
|
||||||
|
|
||||||
|
|
||||||
class GeoIP:
|
class GeoIP:
|
||||||
|
@ -115,13 +108,13 @@ class GeoIP:
|
||||||
async def _lookup(self):
|
async def _lookup(self):
|
||||||
try:
|
try:
|
||||||
self.response_text = await self.strategy.get_response()
|
self.response_text = await self.strategy.get_response()
|
||||||
except LookupError:
|
except aiohttp.ClientError as le:
|
||||||
log.exception("geoip lookup failed")
|
log.warn("geoip lookup failed: %r", le)
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
self.element = ElementTree.fromstring(self.response_text)
|
self.element = ElementTree.fromstring(self.response_text)
|
||||||
except ElementTree.ParseError:
|
except ElementTree.ParseError:
|
||||||
log.exception("parsing %r failed", self.response_text)
|
log.debug("parsing response failed: %r", self.response_text)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from aioresponses import aioresponses
|
from aioresponses import aioresponses
|
||||||
|
|
||||||
from subiquitycore.tests import SubiTestCase
|
from subiquitycore.tests import SubiTestCase
|
||||||
|
@ -96,3 +97,10 @@ class TestGeoIPBadData(SubiTestCase):
|
||||||
mocked.get("https://geoip.ubuntu.com/lookup", body=empty_tz)
|
mocked.get("https://geoip.ubuntu.com/lookup", body=empty_tz)
|
||||||
self.assertFalse(await self.geoip.lookup())
|
self.assertFalse(await self.geoip.lookup())
|
||||||
self.assertIsNone(self.geoip.timezone)
|
self.assertIsNone(self.geoip.timezone)
|
||||||
|
|
||||||
|
async def test_lookup_error(self):
|
||||||
|
with aioresponses() as mocked:
|
||||||
|
mocked.get("https://geoip.ubuntu.com/lookup",
|
||||||
|
exception=aiohttp.ClientError('lookup failure'))
|
||||||
|
self.assertFalse(await self.geoip.lookup())
|
||||||
|
self.assertIsNone(self.geoip.timezone)
|
||||||
|
|
Loading…
Reference in New Issue