@@ -30,15 +30,17 @@ class InvalidInputError(OpenCageGeocodeError):
3030 """
3131 There was a problem with the input you provided.
3232
33- :var bad_value: The value that caused the problem
33+ :var message: Error message describing the bad input.
34+ :var bad_value: The value that caused the problem.
3435 """
3536
36- def __init__ (self , bad_value ):
37+ def __init__ (self , message , bad_value = None ):
3738 super ().__init__ ()
39+ self .message = message
3840 self .bad_value = bad_value
3941
4042 def __unicode__ (self ):
41- return "Input must be a unicode string, not " + repr ( self .bad_value )[: 100 ]
43+ return self .message
4244
4345 __str__ = __unicode__
4446
@@ -238,7 +240,11 @@ def reverse_geocode(self, lat, lng, **kwargs):
238240 :raises RateLimitExceededError: if you have exceeded the number of queries you can make.
239241 : Exception says when you can try again
240242 :raises UnknownError: if something goes wrong with the OpenCage API
243+ :raises InvalidInputError: if the latitude or longitude is out of bounds
241244 """
245+
246+ self ._validate_lat_lng (lat , lng )
247+
242248 return self .geocode (_query_for_reverse_geocoding (lat , lng ), ** kwargs )
243249
244250 async def reverse_geocode_async (self , lat , lng , ** kwargs ):
@@ -253,7 +259,11 @@ async def reverse_geocode_async(self, lat, lng, **kwargs):
253259 :rtype: dict
254260 :raises RateLimitExceededError: if exceeded number of queries you can make. You can try again
255261 :raises UnknownError: if something goes wrong with the OpenCage API
262+ :raises InvalidInputError: if the latitude or longitude is out of bounds
256263 """
264+
265+ self ._validate_lat_lng (lat , lng )
266+
257267 return await self .geocode_async (_query_for_reverse_geocoding (lat , lng ), ** kwargs )
258268
259269 @backoff .on_exception (
@@ -335,12 +345,33 @@ async def _opencage_async_request(self, params):
335345
336346 def _parse_request (self , query , params ):
337347 if not isinstance (query , str ):
338- raise InvalidInputError (bad_value = query )
348+ error_message = "Input must be a unicode string, not " + repr (query )[:100 ]
349+ raise InvalidInputError (error_message , bad_value = query )
339350
340351 data = { 'q' : query , 'key' : self .key }
341352 data .update (params ) # Add user parameters
342353 return data
343354
355+ def _validate_lat_lng (self , lat , lng ):
356+ """
357+ Validate latitude and longitude values.
358+
359+ Raises InvalidInputError if the values are out of bounds.
360+ """
361+ try :
362+ lat_float = float (lat )
363+ if not - 90 <= lat_float <= 90 :
364+ raise InvalidInputError (f"Latitude must be a number between -90 and 90, not { lat } " , bad_value = lat )
365+ except ValueError :
366+ raise InvalidInputError (f"Latitude must be a number between -90 and 90, not { lat } " , bad_value = lat )
367+
368+ try :
369+ lng_float = float (lng )
370+ if not - 180 <= lng_float <= 180 :
371+ raise InvalidInputError (f"Longitude must be a number between -180 and 180, not { lng } " , bad_value = lng )
372+ except ValueError :
373+ raise InvalidInputError (f"Longitude must be a number between -180 and 180, not { lng } " , bad_value = lng )
374+
344375
345376def _query_for_reverse_geocoding (lat , lng ):
346377 """
0 commit comments