From: Sarah Hoffmann Date: Fri, 16 Aug 2024 17:47:48 +0000 (+0200) Subject: CLI: more useful error messages on JSON formatting errors X-Git-Tag: deploy~1^2~3^2~2 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/d648561a87bbe69dd0e9d156bfb8910ed304cbd3 CLI: more useful error messages on JSON formatting errors --- diff --git a/src/nominatim_db/clicmd/api.py b/src/nominatim_db/clicmd/api.py index 3deb7e45..ddc10ff9 100644 --- a/src/nominatim_db/clicmd/api.py +++ b/src/nominatim_db/clicmd/api.py @@ -122,11 +122,17 @@ def _print_output(formatter: napi.FormatDispatcher, result: Any, output = formatter.format_result(result, fmt, options) if formatter.get_content_type(fmt) == CONTENT_JSON: # reformat the result, so it is pretty-printed - json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False) + try: + json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False) + except json.decoder.JSONDecodeError as err: + # Catch the error here, so that data can be debugged, + # when people are developping custom result formatters. + LOG.fatal("Parsing json failed: %s\nUnformatted output:\n%s", err, output) else: sys.stdout.write(output) sys.stdout.write('\n') + class APISearch: """\ Execute a search query.