mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-26 11:08:13 +00:00
translate UsageError in CLI API commands
UsageErrors are handled specially and don't show backtraces unless explicitly requested. We want the same behaviour for errors coming from the API functions.
This commit is contained in:
@@ -180,29 +180,32 @@ class APISearch:
|
|||||||
raise UsageError(f"Unsupported format '{args.format}'. "
|
raise UsageError(f"Unsupported format '{args.format}'. "
|
||||||
'Use --list-formats to see supported formats.')
|
'Use --list-formats to see supported formats.')
|
||||||
|
|
||||||
with napi.NominatimAPI(args.project_dir) as api:
|
try:
|
||||||
params: Dict[str, Any] = {'max_results': args.limit + min(args.limit, 10),
|
with napi.NominatimAPI(args.project_dir) as api:
|
||||||
'address_details': True, # needed for display name
|
params: Dict[str, Any] = {'max_results': args.limit + min(args.limit, 10),
|
||||||
'geometry_output': _get_geometry_output(args),
|
'address_details': True, # needed for display name
|
||||||
'geometry_simplification': args.polygon_threshold,
|
'geometry_output': _get_geometry_output(args),
|
||||||
'countries': args.countrycodes,
|
'geometry_simplification': args.polygon_threshold,
|
||||||
'excluded': args.exclude_place_ids,
|
'countries': args.countrycodes,
|
||||||
'viewbox': args.viewbox,
|
'excluded': args.exclude_place_ids,
|
||||||
'bounded_viewbox': args.bounded,
|
'viewbox': args.viewbox,
|
||||||
'locales': _get_locales(args, api.config.DEFAULT_LANGUAGE)
|
'bounded_viewbox': args.bounded,
|
||||||
}
|
'locales': _get_locales(args, api.config.DEFAULT_LANGUAGE)
|
||||||
|
}
|
||||||
|
|
||||||
if args.query:
|
if args.query:
|
||||||
results = api.search(args.query, **params)
|
results = api.search(args.query, **params)
|
||||||
else:
|
else:
|
||||||
results = api.search_address(amenity=args.amenity,
|
results = api.search_address(amenity=args.amenity,
|
||||||
street=args.street,
|
street=args.street,
|
||||||
city=args.city,
|
city=args.city,
|
||||||
county=args.county,
|
county=args.county,
|
||||||
state=args.state,
|
state=args.state,
|
||||||
postalcode=args.postalcode,
|
postalcode=args.postalcode,
|
||||||
country=args.country,
|
country=args.country,
|
||||||
**params)
|
**params)
|
||||||
|
except napi.UsageError as ex:
|
||||||
|
raise UsageError(ex) from ex
|
||||||
|
|
||||||
if args.dedupe and len(results) > 1:
|
if args.dedupe and len(results) > 1:
|
||||||
results = deduplicate_results(results, args.limit)
|
results = deduplicate_results(results, args.limit)
|
||||||
@@ -260,15 +263,19 @@ class APIReverse:
|
|||||||
if args.lat is None or args.lon is None:
|
if args.lat is None or args.lon is None:
|
||||||
raise UsageError("lat' and 'lon' parameters are required.")
|
raise UsageError("lat' and 'lon' parameters are required.")
|
||||||
|
|
||||||
with napi.NominatimAPI(args.project_dir) as api:
|
layers = _get_layers(args, napi.DataLayer.ADDRESS | napi.DataLayer.POI)
|
||||||
result = api.reverse(napi.Point(args.lon, args.lat),
|
|
||||||
max_rank=zoom_to_rank(args.zoom or 18),
|
try:
|
||||||
layers=_get_layers(args,
|
with napi.NominatimAPI(args.project_dir) as api:
|
||||||
napi.DataLayer.ADDRESS | napi.DataLayer.POI),
|
result = api.reverse(napi.Point(args.lon, args.lat),
|
||||||
address_details=True, # needed for display name
|
max_rank=zoom_to_rank(args.zoom or 18),
|
||||||
geometry_output=_get_geometry_output(args),
|
layers=layers,
|
||||||
geometry_simplification=args.polygon_threshold,
|
address_details=True, # needed for display name
|
||||||
locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
|
geometry_output=_get_geometry_output(args),
|
||||||
|
geometry_simplification=args.polygon_threshold,
|
||||||
|
locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
|
||||||
|
except napi.UsageError as ex:
|
||||||
|
raise UsageError(ex) from ex
|
||||||
|
|
||||||
if args.format == 'debug':
|
if args.format == 'debug':
|
||||||
print(loglib.get_and_disable())
|
print(loglib.get_and_disable())
|
||||||
@@ -324,12 +331,15 @@ class APILookup:
|
|||||||
|
|
||||||
places = [napi.OsmID(o[0], int(o[1:])) for o in args.ids]
|
places = [napi.OsmID(o[0], int(o[1:])) for o in args.ids]
|
||||||
|
|
||||||
with napi.NominatimAPI(args.project_dir) as api:
|
try:
|
||||||
results = api.lookup(places,
|
with napi.NominatimAPI(args.project_dir) as api:
|
||||||
address_details=True, # needed for display name
|
results = api.lookup(places,
|
||||||
geometry_output=_get_geometry_output(args),
|
address_details=True, # needed for display name
|
||||||
geometry_simplification=args.polygon_threshold or 0.0,
|
geometry_output=_get_geometry_output(args),
|
||||||
locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
|
geometry_simplification=args.polygon_threshold or 0.0,
|
||||||
|
locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
|
||||||
|
except napi.UsageError as ex:
|
||||||
|
raise UsageError(ex) from ex
|
||||||
|
|
||||||
if args.format == 'debug':
|
if args.format == 'debug':
|
||||||
print(loglib.get_and_disable())
|
print(loglib.get_and_disable())
|
||||||
@@ -411,17 +421,20 @@ class APIDetails:
|
|||||||
raise UsageError('One of the arguments --node/-n --way/-w '
|
raise UsageError('One of the arguments --node/-n --way/-w '
|
||||||
'--relation/-r --place_id/-p is required/')
|
'--relation/-r --place_id/-p is required/')
|
||||||
|
|
||||||
with napi.NominatimAPI(args.project_dir) as api:
|
try:
|
||||||
locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
|
with napi.NominatimAPI(args.project_dir) as api:
|
||||||
result = api.details(place,
|
locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
|
||||||
address_details=args.addressdetails,
|
result = api.details(place,
|
||||||
linked_places=args.linkedplaces,
|
address_details=args.addressdetails,
|
||||||
parented_places=args.hierarchy,
|
linked_places=args.linkedplaces,
|
||||||
keywords=args.keywords,
|
parented_places=args.hierarchy,
|
||||||
geometry_output=napi.GeometryFormat.GEOJSON
|
keywords=args.keywords,
|
||||||
if args.polygon_geojson
|
geometry_output=napi.GeometryFormat.GEOJSON
|
||||||
else napi.GeometryFormat.NONE,
|
if args.polygon_geojson
|
||||||
locales=locales)
|
else napi.GeometryFormat.NONE,
|
||||||
|
locales=locales)
|
||||||
|
except napi.UsageError as ex:
|
||||||
|
raise UsageError(ex) from ex
|
||||||
|
|
||||||
if args.format == 'debug':
|
if args.format == 'debug':
|
||||||
print(loglib.get_and_disable())
|
print(loglib.get_and_disable())
|
||||||
@@ -466,8 +479,11 @@ class APIStatus:
|
|||||||
raise UsageError(f"Unsupported format '{args.format}'. "
|
raise UsageError(f"Unsupported format '{args.format}'. "
|
||||||
'Use --list-formats to see supported formats.')
|
'Use --list-formats to see supported formats.')
|
||||||
|
|
||||||
with napi.NominatimAPI(args.project_dir) as api:
|
try:
|
||||||
status = api.status()
|
with napi.NominatimAPI(args.project_dir) as api:
|
||||||
|
status = api.status()
|
||||||
|
except napi.UsageError as ex:
|
||||||
|
raise UsageError(ex) from ex
|
||||||
|
|
||||||
if args.format == 'debug':
|
if args.format == 'debug':
|
||||||
print(loglib.get_and_disable())
|
print(loglib.get_and_disable())
|
||||||
|
|||||||
Reference in New Issue
Block a user