From c4f6a80b0c28d1808daf1ab4ba10ad5075ee2b07 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Mon, 19 Jun 2017 12:01:55 +0200 Subject: [PATCH] Fix a StopIteration error in web API --- flatisfy/web/routes/api.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/flatisfy/web/routes/api.py b/flatisfy/web/routes/api.py index 88f40dc..1894bf0 100644 --- a/flatisfy/web/routes/api.py +++ b/flatisfy/web/routes/api.py @@ -51,7 +51,9 @@ def flats_v1(config, db): ] for flat in flats: - if flat["flatisfy_postal_code"]: + try: + assert flat["flatisfy_postal_code"] + postal_code_data = next( x for x in postal_codes.get(flat["flatisfy_constraint"], @@ -63,7 +65,7 @@ def flats_v1(config, db): "name": postal_code_data.name, "gps": (postal_code_data.lat, postal_code_data.lng) } - else: + except (AssertionError, StopIteration): flat["flatisfy_postal_code"] = {} return { @@ -115,7 +117,9 @@ def flat_v1(flat_id, config, db): flat = flat.json_api_repr() - if flat["flatisfy_postal_code"]: + try: + assert flat["flatisfy_postal_code"] + postal_code_data = next( x for x in postal_codes @@ -126,7 +130,7 @@ def flat_v1(flat_id, config, db): "name": postal_code_data.name, "gps": (postal_code_data.lat, postal_code_data.lng) } - else: + except (AssertionError, StopIteration): flat["flatisfy_postal_code"] = {} return { @@ -267,7 +271,9 @@ def search_v1(db, config): ] for flat in flats: - if flat["flatisfy_postal_code"]: + try: + assert flat["flatisfy_postal_code"] + postal_code_data = next( x for x in postal_codes.get(flat["flatisfy_constraint"], @@ -276,10 +282,10 @@ def search_v1(db, config): ) flat["flatisfy_postal_code"] = { "postal_code": flat["flatisfy_postal_code"], - "name": postal_code_data["name"], - "gps": (postal_code_data["lat"], postal_code_data["lng"]) + "name": postal_code_data.name, + "gps": (postal_code_data.lat, postal_code_data.lng) } - else: + except (AssertionError, StopIteration): flat["flatisfy_postal_code"] = {} return {