forked from hans/Nominatim
fix error message for non-existing database
This commit is contained in:
@@ -61,6 +61,6 @@ async def get_status(engine: AsyncEngine) -> StatusResult:
|
|||||||
status.data_updated = await _get_database_date(conn)
|
status.data_updated = await _get_database_date(conn)
|
||||||
status.database_version = await _get_database_version(conn)
|
status.database_version = await _get_database_version(conn)
|
||||||
except asyncpg.PostgresError:
|
except asyncpg.PostgresError:
|
||||||
return StatusResult(700, 'No database')
|
return StatusResult(700, 'Database connection failed')
|
||||||
|
|
||||||
return status
|
return status
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ create = FormatDispatcher()
|
|||||||
|
|
||||||
@create.format_func(StatusResult, 'text')
|
@create.format_func(StatusResult, 'text')
|
||||||
def _format_status_text(result: StatusResult) -> str:
|
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')
|
@create.format_func(StatusResult, 'json')
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ class NominatimV1:
|
|||||||
result = await self.api.status()
|
result = await self.api.status()
|
||||||
|
|
||||||
self.format_response(req, resp, result)
|
self.format_response(req, resp, result)
|
||||||
|
if result.status and req.context.format == 'text':
|
||||||
|
resp.status = 500
|
||||||
|
|
||||||
|
|
||||||
def get_application(project_dir: Path,
|
def get_application(project_dir: Path,
|
||||||
|
|||||||
@@ -61,7 +61,13 @@ async def extract_format(request: sanic.Request) -> Optional[sanic.HTTPResponse]
|
|||||||
async def status(request: sanic.Request) -> sanic.HTTPResponse:
|
async def status(request: sanic.Request) -> sanic.HTTPResponse:
|
||||||
""" Implementation of status endpoint.
|
""" 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,
|
def get_application(project_dir: Path,
|
||||||
|
|||||||
@@ -60,7 +60,12 @@ async def on_status(request: Request) -> Response:
|
|||||||
"""
|
"""
|
||||||
parse_format(request, StatusResult, 'text')
|
parse_format(request, StatusResult, 'text')
|
||||||
result = await request.app.state.API.status()
|
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 = [
|
V1_ROUTES = [
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ def test_status_database_not_found(monkeypatch):
|
|||||||
result = api.status()
|
result = api.status()
|
||||||
|
|
||||||
assert result.status == 700
|
assert result.status == 700
|
||||||
assert result.message == 'No database'
|
assert result.message == 'Database connection failed'
|
||||||
assert result.software_version == version_str()
|
assert result.software_version == version_str()
|
||||||
assert result.database_version is None
|
assert result.database_version is None
|
||||||
assert result.data_updated is None
|
assert result.data_updated is None
|
||||||
|
|||||||
@@ -38,7 +38,11 @@ class TestStatusResultFormat:
|
|||||||
|
|
||||||
|
|
||||||
def test_format_text(self):
|
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):
|
def test_format_json_minimal(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user