Skip to content

Commit deae7e0

Browse files
committed
osm2pgsql-replication: switch to proper use of exceptions
1 parent 6b48926 commit deae7e0

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

scripts/osm2pgsql-replication

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ class Osm2pgsqlProperties:
180180
if seq is None or isinstance(start_at, int):
181181
date = self._get_prop('current_timestamp')
182182
if date is None:
183-
LOG.Fatal("Cannot get timestamp from database. "
184-
"Use --start-at to set an explicit date.")
185-
return None, None, None
183+
raise DBError(1, "Cannot get timestamp from database. "
184+
"Use --start-at to set an explicit date.")
186185

187186
date = from_osm_date(date)
188187
if start_at is not None:
@@ -239,8 +238,7 @@ class LegacyProperties:
239238
osmid = cur.fetchone()[0] if cur.rowcount == 1 else None
240239

241240
if osmid is None:
242-
LOG.fatal("No data found in the database.")
243-
return None, None, None
241+
raise DBError(1, "No data found in the database.")
244242

245243
LOG.debug("Using way id %d for timestamp lookup", osmid)
246244
# Get the way from the API to find the timestamp when it was created.
@@ -251,18 +249,16 @@ class LegacyProperties:
251249
data = json.loads(response.read().decode('utf-8'))
252250

253251
if not data.get('elements') or not 'timestamp' in data['elements'][0]:
254-
LOG.fatal("The way data downloaded from the API does not contain valid data.\n"
255-
"URL used: %s", url)
256-
return None, None, None
252+
raise DBError(1, "The way data downloaded from the API does not contain valid data.\n"
253+
f"URL used: {url}")
257254

258255
date = data['elements'][0]['timestamp']
259256
LOG.debug("Found timestamp %s", date)
260257

261258
try:
262259
date = from_osm_date(date)
263260
except ValueError:
264-
LOG.fatal("Cannot parse timestamp '%s'", date)
265-
return None, None, None
261+
raise DBError(1, f"Cannot parse timestamp '{date}'")
266262

267263
if isinstance(start_at, int):
268264
date -= dt.timedelta(minutes=start_at)
@@ -423,30 +419,23 @@ def init(props, args):
423419
"""
424420
if args.osm_file is None:
425421
base_url, seq, date = props.get_replication_base(args.server, args.start_at)
426-
if base_url is None:
427-
return 1
428422
else:
429423
base_url, seq, date = get_replication_header(args.osm_file)
430424
if base_url is None or (seq is None and date is None):
431-
LOG.fatal("File '%s' has no usable replication headers. Use '--server' instead.", args.osm_file)
432-
return 1
425+
raise DBError(1, f"File '{args.osm_file}' has no usable replication headers. Use '--server' instead.")
433426

434427
repl = ReplicationServer(base_url)
435428
if seq is None:
436429
seq = repl.timestamp_to_sequence(date)
437-
438430
if seq is None:
439-
LOG.fatal("Cannot reach the configured replication service '%s'.\n"
440-
"Does the URL point to a directory containing OSM update data?",
441-
base_url)
442-
return 1
431+
raise DBError(1, f"Cannot reach the configured replication service '{base_url}'.\n"
432+
"Does the URL point to a directory containing OSM update data?")
443433

444434
if date is None:
445435
state = repl.get_state_info(seq)
446436
if state is None:
447-
LOG.fatal("Cannot reach the configured replication service '%s'.\n"
448-
"Does the URL point to a directory containing OSM update data?",
449-
base_url)
437+
raise DBError(1, f"Cannot reach the configured replication service '{base_url}'.\n"
438+
"Does the URL point to a directory containing OSM update data?")
450439
date = from_osm_date(state.timestamp)
451440

452441
props.write_replication_state(base_url, seq, date)
@@ -483,11 +472,7 @@ def update(props, args):
483472
may be missing in the rare case that the replication service stops responding
484473
after the updates have been downloaded.
485474
"""
486-
try:
487-
base_url, seq, ts = props.get_replication_state()
488-
except DBError as err:
489-
LOG.fatal(err.msg)
490-
return 1
475+
base_url, seq, ts = props.get_replication_state()
491476

492477
initial_local_timestamp = ts
493478
LOG.info("Using replication service '%s'.", base_url)
@@ -496,10 +481,8 @@ def update(props, args):
496481
repl = ReplicationServer(base_url)
497482
current = repl.get_state_info()
498483
if current is None:
499-
LOG.fatal("Cannot reach the configured replication service '%s'.\n"
500-
"Does the URL point to a directory containing OSM update data?",
501-
base_url)
502-
return 1
484+
raise DBError(1, f"Cannot reach the configured replication service '{base_url}'.\n"
485+
"Does the URL point to a directory containing OSM update data?")
503486

504487
if seq >= current.sequence:
505488
LOG.info("Database already up-to-date.")
@@ -573,11 +556,7 @@ def update(props, args):
573556
break
574557

575558
update_duration_sec = (dt.datetime.now(dt.timezone.utc) - update_started).total_seconds()
576-
try:
577-
_base_url, _seq, current_local_timestamp = props.get_replication_state()
578-
except DBError as err:
579-
LOG.fatal(err.msg)
580-
return 1
559+
_base_url, _seq, current_local_timestamp = props.get_replication_state()
581560

582561
total_applied_changes_duration_sec = (current_local_timestamp - initial_local_timestamp).total_seconds()
583562
LOG.debug("It took %s (%d sec) to apply %s (%d sec) of changes. This is a speed of ×%.1f.",
@@ -710,7 +689,12 @@ def main():
710689
'Database needs to be imported in --slim mode.')
711690
return 1
712691

713-
return args.handler(props, args)
692+
try:
693+
return args.handler(props, args)
694+
except DBError as err:
695+
LOG.fatal(err.msg)
696+
697+
return 1
714698

715699

716700
if __name__ == '__main__':

0 commit comments

Comments
 (0)