X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/c314a3092c5b51c7782015f6fa9ac093b46fa174..83013f819b463b14332854dd80cc47629bea0d1a:/src/nominatim_api/v1/format.py diff --git a/src/nominatim_api/v1/format.py b/src/nominatim_api/v1/format.py index e74b61e1..478c7207 100644 --- a/src/nominatim_api/v1/format.py +++ b/src/nominatim_api/v1/format.py @@ -19,12 +19,38 @@ from ..localization import Locales from ..result_formatting import FormatDispatcher from .classtypes import ICONS from . import format_json, format_xml +from .. import logging as loglib +from ..server import content_types as ct class RawDataList(List[Dict[str, Any]]): """ Data type for formatting raw data lists 'as is' in json. """ -dispatch = FormatDispatcher() +dispatch = FormatDispatcher({'text': ct.CONTENT_TEXT, + 'xml': ct.CONTENT_XML, + 'debug': ct.CONTENT_HTML}) + +@dispatch.error_format_func +def _format_error(content_type: str, msg: str, status: int) -> str: + if content_type == ct.CONTENT_XML: + return f""" + + {status} + {msg} + + """ + + if content_type == ct.CONTENT_JSON: + return f"""{{"error":{{"code":{status},"message":"{msg}"}}}}""" + + if content_type == ct.CONTENT_HTML: + loglib.log().section('Execution error') + loglib.log().var_dump('Status', status) + loglib.log().var_dump('Message', msg) + return loglib.get_and_disable() + + return f"ERROR {status}: {msg}" + @dispatch.format_func(StatusResult, 'text') def _format_status_text(result: StatusResult, _: Mapping[str, Any]) -> str: