Skip to content

Commit dad00de

Browse files
authored
TRAN-7530: Add none checks and similar defenses against exceptions (#56)
* TRAN-7530: default return value * TRAN-7530: check none * TRAN-7530: default value * TRAN-7530: avoids index error * TRAN-7530: avoid nonetype ref * TRAN-7530: return empty array * TRAN-7530: handle nonexistent keys
1 parent 6576540 commit dad00de

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

gtfs_realtime_translators/translators/mbta.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class MbtaGtfsRealtimeTranslator:
1111

1212
def __call__(self, data):
1313
json_data = json.loads(data)
14-
predictions = json_data['data']
15-
static_relationships = json_data['included']
16-
static_data = self.__get_static_data(static_relationships)
17-
entities = self.__make_trip_updates(predictions, static_data)
14+
entities = []
15+
predictions = json_data.get('data')
16+
static_relationships = json_data.get('included')
17+
if predictions and static_relationships:
18+
static_data = self.__get_static_data(static_relationships)
19+
entities = self.__make_trip_updates(predictions, static_data)
1820
return FeedMessage.create(entities=entities)
1921

2022
@classmethod

gtfs_realtime_translators/translators/njt_bus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def __skip_processing(cls, stop_id, filtered_stops):
3535
@classmethod
3636
def __make_trip_updates(cls, data, filtered_stops):
3737
trip_updates = []
38-
trips = data['SCHEDULEROWSET'].values()
38+
schedule_row_set = data.get('SCHEDULEROWSET', {})
39+
trips = schedule_row_set.values()
3940

4041
for value in trips:
4142
for idx, item_entry in enumerate(value):

gtfs_realtime_translators/translators/septa_regional_rail.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def __call__(self, data):
4141
raise ValueError('root_key: unexpected format')
4242

4343
arrivals_body = json_data[root_key]
44-
northbound = [ direction_list['Northbound'] for direction_list in arrivals_body if [*direction_list][0] == 'Northbound' ][0]
45-
southbound = [ direction_list['Southbound'] for direction_list in arrivals_body if [*direction_list][0] == 'Southbound' ][0]
44+
northbound = self.get_arrivals_from_direction_list('Northbound', arrivals_body)
45+
southbound = self.get_arrivals_from_direction_list('Southbound', arrivals_body)
4646
arrivals = northbound + southbound
4747

4848
transformed_arrivals = [ self.transform_arrival(arrival) for arrival in arrivals ]
@@ -51,6 +51,19 @@ def __call__(self, data):
5151
entities = [ self.__make_trip_update(idx, self.stop_id, arrival) for idx, arrival in enumerate(filtered_arrivals) ]
5252
return FeedMessage.create(entities=entities)
5353

54+
@classmethod
55+
def get_arrivals_from_direction_list(cls, direction_string, arrivals_body):
56+
arrivals = []
57+
for direction_list in arrivals_body:
58+
# When there are no arrivals, the API gives us an empty list instead
59+
# of a dictionary
60+
if isinstance(direction_list, dict) \
61+
and [*direction_list][0] == direction_string:
62+
arrivals.append(direction_list[direction_string])
63+
if arrivals:
64+
return arrivals[0]
65+
return []
66+
5467
@classmethod
5568
def calculate_time_at(cls, **kwargs):
5669
now = pendulum.now()

gtfs_realtime_translators/translators/wcdot_bus.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ def generate_trip_updates(self, entities):
3131
arrival = update.get("arrival")
3232
departure = update.get("departure")
3333
if stop_id == self.stop_id:
34-
arrival_delay = arrival.get('delay',None)
35-
departure_delay = departure.get('delay',None)
34+
arrival_delay = None
35+
departure_delay = None
36+
if arrival:
37+
arrival_delay = arrival.get('delay',None)
38+
if departure:
39+
departure_delay = departure.get('delay',None)
3640
trip_update = TripUpdate.create(
3741
entity_id=entity_id,
3842
arrival_delay=arrival_delay,

0 commit comments

Comments
 (0)