google-maps-services-python icon indicating copy to clipboard operation
google-maps-services-python copied to clipboard

Issue with maps.static_map() error handling

Open billythedummy opened this issue 5 years ago • 1 comments

Is your feature request related to a problem? Please describe. Because maps.static_map() returns iter_content() of the request directly, exceptions not caught by client._request() may result in it returning the raw HTTP text of an error response without any indication of the error. See "Additional Context" below for examples of content received.

Describe the solution you'd like I think it'd be a good idea to modify client._request() to handle unretryable HTTP errors as well. Perhaps below

https://github.com/googlemaps/google-maps-services-python/blob/5253cbc8b7e47bb1f5c1774b2a30705b158fcd8c/googlemaps/client.py#L295-L299

Something like

elif response.status_code >= 400:
   # raise ApiError or other appropriate exception

could be added

Describe alternatives you've considered My current hacky solution is to use imghdr to check if the saved bytes constitute a valid image file:

import imghdr
...
# save static_map() content as described in https://github.com/googlemaps/google-maps-services-python/blob/5253cbc8b7e47bb1f5c1774b2a30705b158fcd8c/googlemaps/maps.py#L190-L196
...
if not imghdr.what(saved_file):
  # handle error

Additional context Examples of content received from erroneous calls to static_map()

The Google Maps Platform server rejected your request. This API project is not authorized to use this API.
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 413 (Request Entity Too Large)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>413.</b> <ins>That’s an error.</ins>
  <p>Your client issued a request that was too large.
 <script>
  (function() { /*

 Copyright The Closure Library Authors.
 SPDX-License-Identifier: Apache-2.0
*/
var c=function(a,d,b){a=a+"=deleted; path="+d;null!=b&&(a+="; domain="+b);document.cookie=a+"; expires=Thu, 01 Jan 1970 00:00:00 GMT"};var g=function(a){var d=e,b=location.hostname;c(d,a,null);c(d,a,b);for(var f=0;;){f=b.indexOf(".",f+1);if(0>f)break;c(d,a,b.substring(f+1))}};var h;if(4E3<unescape(encodeURI(document.cookie)).length){for(var k=document.cookie.split(";"),l=[],m=0;m<k.length;m++){var n=k[m].match(/^\s*([^=]+)/);n&&l.push(n[1])}for(var p=0;p<l.length;p++){var e=l[p];g("/");for(var q=location.pathname,r=0;;){r=q.indexOf("/",r+1);if(0>r)break;var t=q.substring(0,r);g(t);g(t+"/")}"/"!=q.charAt(q.length-1)&&(g(q),g(q+"/"))}h=!0}else h=!1;
h&&setTimeout(function(){if(history.replaceState){var a=location.href;history.replaceState(null,"","/");location.replace(a)}},1E3); })();

</script>
 <ins>That’s all we know.</ins>

billythedummy avatar Sep 09 '20 13:09 billythedummy

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

stale[bot] avatar Jan 16 '21 10:01 stale[bot]

This seems like it might be a duplicate of #447 - closing this one since that issue comes with a proposed fix PR. If you think this issue is distinct, feel free to re-open. You're also welcome to propose a pull request.

wangela avatar Jan 26 '23 00:01 wangela