From 93b9288c3051dd828b16532cac9db87964587afb Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 7 Dec 2022 20:05:24 +0100 Subject: [PATCH] fix error message for non-existing database --- nominatim/apicmd/status.py | 2 +- nominatim/result_formatter/v1.py | 5 ++++- nominatim/server/falcon/server.py | 2 ++ nominatim/server/sanic/server.py | 8 +++++++- nominatim/server/starlette/server.py | 7 ++++++- test/python/api/test_api_status.py | 2 +- test/python/result_formatter/test_v1.py | 6 +++++- 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/nominatim/apicmd/status.py b/nominatim/apicmd/status.py index b5ee9cb9..07a1c321 100644 --- a/nominatim/apicmd/status.py +++ b/nominatim/apicmd/status.py @@ -61,6 +61,6 @@ async def get_status(engine: AsyncEngine) -> StatusResult: status.data_updated = await _get_database_date(conn) status.database_version = await _get_database_version(conn) except asyncpg.PostgresError: - return StatusResult(700, 'No database') + return StatusResult(700, 'Database connection failed') return status diff --git a/nominatim/result_formatter/v1.py b/nominatim/result_formatter/v1.py index 081f451e..6d25ce6c 100644 --- a/nominatim/result_formatter/v1.py +++ b/nominatim/result_formatter/v1.py @@ -18,7 +18,10 @@ create = FormatDispatcher() @create.format_func(StatusResult, 'text') def _format_status_text(result: StatusResult) -> str: - return result.message + if result.status: + return f"ERROR: {result.message}" + + return 'OK' @create.format_func(StatusResult, 'json') diff --git a/nominatim/server/falcon/server.py b/nominatim/server/falcon/server.py index b7b1aaa5..81e6ed39 100644 --- a/nominatim/server/falcon/server.py +++ b/nominatim/server/falcon/server.py @@ -65,6 +65,8 @@ class NominatimV1: result = await self.api.status() self.format_response(req, resp, result) + if result.status and req.context.format == 'text': + resp.status = 500 def get_application(project_dir: Path, diff --git a/nominatim/server/sanic/server.py b/nominatim/server/sanic/server.py index d00676b9..74841f3f 100644 --- a/nominatim/server/sanic/server.py +++ b/nominatim/server/sanic/server.py @@ -61,7 +61,13 @@ async def extract_format(request: sanic.Request) -> Optional[sanic.HTTPResponse] async def status(request: sanic.Request) -> sanic.HTTPResponse: """ Implementation of status endpoint. """ - return api_response(request,await request.app.ctx.api.status()) + result = await request.app.ctx.api.status() + response = api_response(request, result) + + if request.ctx.format == 'text' and result.status: + response.status = 500 + + return response def get_application(project_dir: Path, diff --git a/nominatim/server/starlette/server.py b/nominatim/server/starlette/server.py index bed82f70..41ad899c 100644 --- a/nominatim/server/starlette/server.py +++ b/nominatim/server/starlette/server.py @@ -60,7 +60,12 @@ async def on_status(request: Request) -> Response: """ parse_format(request, StatusResult, 'text') result = await request.app.state.API.status() - return format_response(request, result) + response = format_response(request, result) + + if request.state.format == 'text' and result.status: + response.status_code = 500 + + return response V1_ROUTES = [ diff --git a/test/python/api/test_api_status.py b/test/python/api/test_api_status.py index aae5055e..38c0fa7f 100644 --- a/test/python/api/test_api_status.py +++ b/test/python/api/test_api_status.py @@ -54,7 +54,7 @@ def test_status_database_not_found(monkeypatch): result = api.status() assert result.status == 700 - assert result.message == 'No database' + assert result.message == 'Database connection failed' assert result.software_version == version_str() assert result.database_version is None assert result.data_updated is None diff --git a/test/python/result_formatter/test_v1.py b/test/python/result_formatter/test_v1.py index da944c25..5b7d2bfc 100644 --- a/test/python/result_formatter/test_v1.py +++ b/test/python/result_formatter/test_v1.py @@ -38,7 +38,11 @@ class TestStatusResultFormat: def test_format_text(self): - assert self.formatter.format(StatusResult(0, 'message here'), 'text') == 'message here' + assert self.formatter.format(StatusResult(0, 'message here'), 'text') == 'OK' + + + def test_format_text(self): + assert self.formatter.format(StatusResult(500, 'message here'), 'text') == 'ERROR: message here' def test_format_json_minimal(self): -- 2.39.5