ericnewton76/gmaps-api-net

Exceptions thrown for non-200 code

samrueby opened this issue · 0 comments

When Google responds with an error, for example with the Geocoding API, they respond with JSON describing the problem, but also set the HTTP status code to 400. gmaps-api-net uses HttpClient, which throws for non-200 codes, and doesn't catch them.

Example Google response with 400 code:
{
"error_message" : "Invalid request. Missing the 'address', 'components', 'latlng' or 'place_id' parameter.",
"results" : [],
"status" : "INVALID_REQUEST"
}

Stack trace when this happens:
System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request).
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Maps.Internal.MapsHttp.d__4`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Maps.Internal.MapsHttp.Get[T](Uri uri)

What should be happening instead:
gmaps-api-net should be handling the non-200 code and instead returning the appropriately deserialized GeocodeResponse type, which will have the ServiceResponseStatus Status field set to InvalidRequest.