1616from .geocoding import (
1717 approx_street_from_address ,
1818 geocode_address ,
19+ GeocodingConfigError ,
20+ GeocodingProviderError ,
1921 reverse_geocode ,
2022 rough_location_from_address ,
2123)
@@ -101,7 +103,7 @@ def upsert_listing(payload: ListingUpsert, db: DbDep) -> Listing:
101103 if candidates :
102104 existing .lat = existing .lat or candidates [0 ].lat
103105 existing .lng = existing .lng or candidates [0 ].lng
104- except HTTPError :
106+ except ( HTTPError , GeocodingConfigError , GeocodingProviderError ) :
105107 pass
106108
107109 if (
@@ -114,7 +116,7 @@ def upsert_listing(payload: ListingUpsert, db: DbDep) -> Listing:
114116 rough = rough_location_from_address (rev .address )
115117 if rough :
116118 existing .location_text = rough
117- except HTTPError :
119+ except ( HTTPError , GeocodingConfigError , GeocodingProviderError ) :
118120 pass
119121
120122 db .add (existing )
@@ -145,15 +147,15 @@ def upsert_listing(payload: ListingUpsert, db: DbDep) -> Listing:
145147 if candidates :
146148 listing .lat = listing .lat or candidates [0 ].lat
147149 listing .lng = listing .lng or candidates [0 ].lng
148- except HTTPError :
150+ except ( HTTPError , GeocodingConfigError , GeocodingProviderError ) :
149151 pass
150152 if listing .location_text is None and listing .lat is not None and listing .lng is not None :
151153 try :
152154 rev = reverse_geocode (listing .lat , listing .lng , zoom = 10 )
153155 rough = rough_location_from_address (rev .address )
154156 if rough :
155157 listing .location_text = rough
156- except HTTPError :
158+ except ( HTTPError , GeocodingConfigError , GeocodingProviderError ) :
157159 pass
158160 db .add (listing )
159161 db .commit ()
@@ -184,12 +186,18 @@ def upsert_target(payload: TargetUpsert, db: DbDep) -> Target:
184186 lat = payload .lat
185187 lng = payload .lng
186188 address = payload .address .strip () if isinstance (payload .address , str ) else None
189+ if address == "" :
190+ address = None
187191
188192 if lat is None or lng is None :
189193 try :
190194 candidates = geocode_address (address or "" , limit = 1 )
191195 except HTTPError as e :
192196 raise HTTPException (status_code = 502 , detail = str (e )) from e
197+ except GeocodingConfigError as e :
198+ raise HTTPException (status_code = 500 , detail = str (e )) from e
199+ except GeocodingProviderError as e :
200+ raise HTTPException (status_code = 502 , detail = str (e )) from e
193201 if not candidates :
194202 raise HTTPException (status_code = 404 , detail = "Address not found" )
195203 lat = candidates [0 ].lat
@@ -244,6 +252,10 @@ def api_geocode(
244252 results = geocode_address (query , limit = limit )
245253 except HTTPError as e :
246254 raise HTTPException (status_code = 502 , detail = str (e )) from e
255+ except GeocodingConfigError as e :
256+ raise HTTPException (status_code = 500 , detail = str (e )) from e
257+ except GeocodingProviderError as e :
258+ raise HTTPException (status_code = 502 , detail = str (e )) from e
247259 return [GeocodeResultOut (display_name = r .display_name , lat = r .lat , lng = r .lng ) for r in results ]
248260
249261
@@ -257,6 +269,10 @@ def api_reverse_geocode(
257269 rev = reverse_geocode (lat , lng , zoom = zoom )
258270 except HTTPError as e :
259271 raise HTTPException (status_code = 502 , detail = str (e )) from e
272+ except GeocodingConfigError as e :
273+ raise HTTPException (status_code = 500 , detail = str (e )) from e
274+ except GeocodingProviderError as e :
275+ raise HTTPException (status_code = 502 , detail = str (e )) from e
260276 return ReverseGeocodeOut (
261277 display_name = rev .display_name ,
262278 rough_location = rough_location_from_address (rev .address ),
0 commit comments