introduce custom UsageError

This is a exception to be thrown when the error occures because
of bad user data. We don't want to print a full stack trace in
these cases but just tell the user what went wrong.
This commit is contained in:
Sarah Hoffmann
2021-01-30 16:20:10 +01:00
parent 45ea73913f
commit e629a175ed
11 changed files with 45 additions and 20 deletions

View File

@@ -14,6 +14,7 @@ from .config import Configuration
from .tools.exec_utils import run_legacy_script, run_api_script
from .db.connection import connect
from .db import status
from .errors import UsageError
LOG = logging.getLogger()
@@ -89,7 +90,16 @@ class CommandlineParser:
args.config = Configuration(args.project_dir, args.data_dir / 'settings')
return args.command.run(args)
try:
return args.command.run(args)
except UsageError as e:
log = logging.getLogger()
if log.isEnabledFor(logging.DEBUG):
raise # use Python's exception printing
log.fatal('FATAL: ' + str(e))
# If we get here, then execution has failed in some way.
return 1
def _osm2pgsql_options_from_args(args, default_cache, default_threads):
@@ -292,12 +302,12 @@ class UpdateReplication:
"Please check install documentation "
"(https://nominatim.org/release-docs/latest/admin/Import-and-Update#"
"setting-up-the-update-process).")
raise RuntimeError("Invalid replication update interval setting.")
raise UsageError("Invalid replication update interval setting.")
if not args.once:
if not args.do_index:
LOG.fatal("Indexing cannot be disabled when running updates continuously.")
raise RuntimeError("Bad arguments.")
raise UsageError("Bad argument '--no-index'.")
recheck_interval = args.config.get_int('REPLICATION_RECHECK_INTERVAL')
while True:

View File

@@ -7,6 +7,8 @@ from pathlib import Path
from dotenv import dotenv_values
from .errors import UsageError
LOG = logging.getLogger()
class Configuration:
@@ -57,7 +59,7 @@ class Configuration:
return int(self.__getattr__(name))
except ValueError:
LOG.fatal("Invalid setting NOMINATIM_%s. Needs to be a number.", name)
raise
raise UsageError("Configuration error.")
def get_libpq_dsn(self):

View File

@@ -27,7 +27,7 @@ class _Cursor(psycopg2.extras.DictCursor):
self.execute(sql, args)
if self.rowcount != 1:
raise ValueError("Query did not return a single row.")
raise RuntimeError("Query did not return a single row.")
return self.fetchone()[0]

View File

@@ -6,6 +6,7 @@ import logging
import re
from ..tools.exec_utils import get_url
from ..errors import UsageError
LOG = logging.getLogger()
@@ -19,7 +20,7 @@ def compute_database_date(conn):
if osmid is None:
LOG.fatal("No data found in the database.")
raise RuntimeError("No data found in the database.")
raise UsageError("No data found in the database.")
LOG.info("Using node id %d for timestamp lookup", osmid)
# Get the node from the API to find the timestamp when it was created.
@@ -31,7 +32,7 @@ def compute_database_date(conn):
if match is None:
LOG.fatal("The node data downloaded from the API does not contain valid data.\n"
"URL used: %s", node_url)
raise RuntimeError("Bad API data.")
raise UsageError("Bad API data.")
LOG.debug("Found timestamp %s", match[1])

9
nominatim/errors.py Normal file
View File

@@ -0,0 +1,9 @@
"""
Custom exception and error classes for Nominatim.
"""
class UsageError(Exception):
""" An error raised because of bad user input. This error will usually
not cause a stack trace to be printed unless debugging is enabled.
"""
pass

View File

@@ -11,6 +11,7 @@ from osmium import WriteHandler
from ..db import status
from .exec_utils import run_osm2pgsql
from ..errors import UsageError
LOG = logging.getLogger()
@@ -31,7 +32,7 @@ def init_replication(conn, base_url):
LOG.fatal("Cannot reach the configured replication service '%s'.\n"
"Does the URL point to a directory containing OSM update data?",
base_url)
raise RuntimeError("Failed to reach replication service")
raise UsageError("Failed to reach replication service")
status.set_status(conn, date=date, seq=seq)
@@ -80,7 +81,7 @@ def update(conn, options):
if startseq is None:
LOG.error("Replication not set up. "
"Please run 'nominatim replication --init' first.")
raise RuntimeError("Replication not set up.")
raise UsageError("Replication not set up.")
if not indexed and options['indexed_only']:
LOG.info("Skipping update. There is data that needs indexing.")