From e629a175ed0a1c54398622251f56d56baeef768f Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sat, 30 Jan 2021 16:20:10 +0100 Subject: [PATCH] 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. --- nominatim/cli.py | 16 +++++++++++++--- nominatim/config.py | 4 +++- nominatim/db/connection.py | 2 +- nominatim/db/status.py | 5 +++-- nominatim/errors.py | 9 +++++++++ nominatim/tools/replication.py | 5 +++-- test/python/test_cli.py | 7 +++---- test/python/test_config.py | 5 +++-- test/python/test_db_connection.py | 2 +- test/python/test_db_status.py | 5 +++-- test/python/test_tools_replication.py | 5 +++-- 11 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 nominatim/errors.py diff --git a/nominatim/cli.py b/nominatim/cli.py index f02277ae..cc591ea5 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -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: diff --git a/nominatim/config.py b/nominatim/config.py index 271d2d4d..4de2052e 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -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): diff --git a/nominatim/db/connection.py b/nominatim/db/connection.py index 8e75d7a2..4d30151d 100644 --- a/nominatim/db/connection.py +++ b/nominatim/db/connection.py @@ -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] diff --git a/nominatim/db/status.py b/nominatim/db/status.py index 9d7344c4..75da3c16 100644 --- a/nominatim/db/status.py +++ b/nominatim/db/status.py @@ -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]) diff --git a/nominatim/errors.py b/nominatim/errors.py new file mode 100644 index 00000000..80181aba --- /dev/null +++ b/nominatim/errors.py @@ -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 diff --git a/nominatim/tools/replication.py b/nominatim/tools/replication.py index 04f1c45b..c7d0d3e5 100644 --- a/nominatim/tools/replication.py +++ b/nominatim/tools/replication.py @@ -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.") diff --git a/test/python/test_cli.py b/test/python/test_cli.py index fc2454cd..c4f3ef36 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -13,6 +13,7 @@ import nominatim.cli import nominatim.indexer.indexer import nominatim.tools.refresh import nominatim.tools.replication +from nominatim.errors import UsageError def call_nominatim(*args): return nominatim.cli.nominatim(module_dir='build/module', @@ -150,16 +151,14 @@ def test_replication_command(monkeypatch, temp_db, params, func): def test_replication_update_bad_interval(monkeypatch, temp_db): monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx') - with pytest.raises(ValueError): - call_nominatim('replication') + assert call_nominatim('replication') == 1 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db): monkeypatch.setenv('NOMINATIM_REPLICATION_URL', 'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates') - with pytest.raises(RuntimeError, match='Invalid replication.*'): - call_nominatim('replication') + assert call_nominatim('replication') == 1 @pytest.mark.parametrize("state, retval", [ diff --git a/test/python/test_config.py b/test/python/test_config.py index f9fefeb2..4578be13 100644 --- a/test/python/test_config.py +++ b/test/python/test_config.py @@ -7,6 +7,7 @@ import tempfile import pytest from nominatim.config import Configuration +from nominatim.errors import UsageError DEFCFG_DIR = Path(__file__) / '..' / '..' / '..' / 'settings' @@ -123,7 +124,7 @@ def test_get_int_bad_values(monkeypatch, value): monkeypatch.setenv('NOMINATIM_FOOBAR', value) - with pytest.raises(ValueError): + with pytest.raises(UsageError): config.get_int('FOOBAR') @@ -132,7 +133,7 @@ def test_get_int_empty(): assert config.DATABASE_MODULE_PATH == '' - with pytest.raises(ValueError): + with pytest.raises(UsageError): config.get_int('DATABASE_MODULE_PATH') diff --git a/test/python/test_db_connection.py b/test/python/test_db_connection.py index 5c484558..ef1ae741 100644 --- a/test/python/test_db_connection.py +++ b/test/python/test_db_connection.py @@ -28,5 +28,5 @@ def test_cursor_scalar(db, temp_db_cursor): def test_cursor_scalar_many_rows(db): with db.cursor() as cur: - with pytest.raises(ValueError): + with pytest.raises(RuntimeError): cur.scalar('SELECT * FROM pg_tables') diff --git a/test/python/test_db_status.py b/test/python/test_db_status.py index 1a538aec..399a0036 100644 --- a/test/python/test_db_status.py +++ b/test/python/test_db_status.py @@ -6,9 +6,10 @@ import datetime as dt import pytest import nominatim.db.status +from nominatim.errors import UsageError def test_compute_database_date_place_empty(status_table, place_table, temp_db_conn): - with pytest.raises(RuntimeError): + with pytest.raises(UsageError): nominatim.db.status.compute_database_date(temp_db_conn) OSM_NODE_DATA = """\ @@ -44,7 +45,7 @@ def test_compute_database_broken_api(monkeypatch, status_table, place_row, temp_ monkeypatch.setattr(nominatim.db.status, "get_url", mock_url) - with pytest.raises(RuntimeError): + with pytest.raises(UsageError): date = nominatim.db.status.compute_database_date(temp_db_conn) diff --git a/test/python/test_tools_replication.py b/test/python/test_tools_replication.py index b94563ff..156385ad 100644 --- a/test/python/test_tools_replication.py +++ b/test/python/test_tools_replication.py @@ -9,6 +9,7 @@ from osmium.replication.server import OsmosisState import nominatim.tools.replication import nominatim.db.status as status +from nominatim.errors import UsageError OSM_NODE_DATA = """\ @@ -24,7 +25,7 @@ def test_init_replication_bad_base_url(monkeypatch, status_table, place_row, tem monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA) - with pytest.raises(RuntimeError, match="Failed to reach replication service"): + with pytest.raises(UsageError, match="Failed to reach replication service"): nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io') @@ -90,7 +91,7 @@ def update_options(tmpdir): max_diff_size=1) def test_update_empty_status_table(status_table, temp_db_conn): - with pytest.raises(RuntimeError): + with pytest.raises(UsageError): nominatim.tools.replication.update(temp_db_conn, {}) -- 2.39.5