Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with maps.static_map() error handling #381

Closed
billythedummy opened this issue Sep 9, 2020 · 2 comments
Closed

Issue with maps.static_map() error handling #381

billythedummy opened this issue Sep 9, 2020 · 2 comments
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@billythedummy
Copy link

billythedummy commented Sep 9, 2020

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

if response.status_code in _RETRIABLE_STATUSES:
# Retry request.
return self._request(url, params, first_request_time,
retry_counter + 1, base_url, accepts_clientid,
extract_body, requests_kwargs, post_json)

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 billythedummy added triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Sep 9, 2020
@stale
Copy link

stale bot commented Jan 16, 2021

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 stale bot added the stale label Jan 16, 2021
@jpoehnelt jpoehnelt added priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed triage me I really want to be triaged. labels Jan 28, 2021
@stale stale bot removed the stale label Jan 28, 2021
@jpoehnelt jpoehnelt removed the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jan 28, 2021
@jpoehnelt jpoehnelt removed their assignment Jan 18, 2023
@wangela
Copy link
Member

wangela commented Jan 26, 2023

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 wangela closed this as completed Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants