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()
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):
"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:
from dotenv import dotenv_values
+from .errors import UsageError
+
LOG = logging.getLogger()
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):
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]
import re
from ..tools.exec_utils import get_url
+from ..errors import UsageError
LOG = logging.getLogger()
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.
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])
--- /dev/null
+"""
+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
from ..db import status
from .exec_utils import run_osm2pgsql
+from ..errors import UsageError
LOG = logging.getLogger()
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)
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.")
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',
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", [
import pytest
from nominatim.config import Configuration
+from nominatim.errors import UsageError
DEFCFG_DIR = Path(__file__) / '..' / '..' / '..' / 'settings'
monkeypatch.setenv('NOMINATIM_FOOBAR', value)
- with pytest.raises(ValueError):
+ with pytest.raises(UsageError):
config.get_int('FOOBAR')
assert config.DATABASE_MODULE_PATH == ''
- with pytest.raises(ValueError):
+ with pytest.raises(UsageError):
config.get_int('DATABASE_MODULE_PATH')
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')
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 = """\
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)
import nominatim.tools.replication
import nominatim.db.status as status
+from nominatim.errors import UsageError
OSM_NODE_DATA = """\
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
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')
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, {})