Repository: googleapis/google-api-python-client

Issue:
NameError: name 'serviceName' is not defined
  File "oauth2client/oauth2client/util.py", line 142, in positional_wrapper
    return wrapped(_args, *_kwargs)
  File "google-api-python-client/googleapiclient/discovery.py", line 193, in build
    content = _retrieve_discovery_doc(requested_url, http, cache_discovery, cache)
  File "googleapiclient/discovery.py", line 236, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName,
NameError: name 'serviceName' is not defined

The same for `version` too, not passed to this function
Other http errors are also not handled.
I suggest you move out the codes inside function `_retrieve_discovery_doc`, and remove this function because it's only used once.
The code is messy



Discussion:
@tmatsuo, what do you make of this?


Resolving patch:
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -29,6 +29,7 @@
     ]
 
 from six import StringIO
+from six.moves import http_client
 from six.moves.urllib.parse import urlencode, urlparse, urljoin, \
   urlunparse, parse_qsl
 
@@ -190,7 +191,15 @@ def build(serviceName,
 
   requested_url = uritemplate.expand(discoveryServiceUrl, params)
 
-  content = _retrieve_discovery_doc(requested_url, http, cache_discovery, cache)
+  try:
+    content = _retrieve_discovery_doc(requested_url, http, cache_discovery,
+                                      cache)
+  except HttpError as e:
+    if e.resp.status == http_client.NOT_FOUND:
+      raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName,
+                                                               version))
+    else:
+      raise e
 
   return build_from_document(content, base=discoveryServiceUrl, http=http,
       developerKey=developerKey, model=model, requestBuilder=requestBuilder,
@@ -232,9 +241,6 @@ def _retrieve_discovery_doc(url, http, cache_discovery, cache=None):
 
   resp, content = http.request(actual_url)
 
-  if resp.status == 404:
-    raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName,
-                                                             version))
   if resp.status >= 400:
     raise HttpError(resp, content, uri=actual_url)
 


Test patch:
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -59,6 +59,7 @@
 from googleapiclient.errors import MediaUploadSizeError
 from googleapiclient.errors import ResumableUploadError
 from googleapiclient.errors import UnacceptableMimeTypeError
+from googleapiclient.errors import UnknownApiNameOrVersion
 from googleapiclient.http import BatchHttpRequest
 from googleapiclient.http import HttpMock
 from googleapiclient.http import HttpMockSequence
@@ -347,6 +348,13 @@ def test_failed_to_parse_discovery_json(self):
     except InvalidJsonError:
       pass
 
+  def test_unknown_api_name_or_version(self):
+      http = HttpMockSequence([
+        ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()),
+      ])
+      with self.assertRaises(UnknownApiNameOrVersion):
+        plus = build('plus', 'v1', http=http, cache_discovery=False)
+
 
 class DiscoveryFromDocument(unittest.TestCase):