Added exception around parsing the discovery doc.

This commit is contained in:
Joe Gregorio
2011-03-04 16:16:55 -05:00
parent 93841708c5
commit c0e0fe90da
3 changed files with 18 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ from http import HttpRequest
from anyjson import simplejson from anyjson import simplejson
from model import JsonModel from model import JsonModel
from errors import UnknownLinkType from errors import UnknownLinkType
from errors import HttpError
URITEMPLATE = re.compile('{[^}]*}') URITEMPLATE = re.compile('{[^}]*}')
VARNAME = re.compile('[a-zA-Z0-9_-]+') VARNAME = re.compile('[a-zA-Z0-9_-]+')
@@ -107,7 +108,10 @@ def build(serviceName, version,
requested_url = uritemplate.expand(discoveryServiceUrl, params) requested_url = uritemplate.expand(discoveryServiceUrl, params)
logging.info('URL being requested: %s' % requested_url) logging.info('URL being requested: %s' % requested_url)
resp, content = http.request(requested_url) resp, content = http.request(requested_url)
try:
service = simplejson.loads(content) service = simplejson.loads(content)
except ValueError, e:
raise HttpError(resp, content)
fn = os.path.join(os.path.dirname(__file__), 'contrib', fn = os.path.join(os.path.dirname(__file__), 'contrib',
serviceName, 'future.json') serviceName, 'future.json')

View File

@@ -0,0 +1 @@
{

View File

@@ -34,6 +34,7 @@ except ImportError:
from apiclient.discovery import build, key2param from apiclient.discovery import build, key2param
from apiclient.http import HttpMock from apiclient.http import HttpMock
from apiclient.errors import HttpError
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
@@ -48,6 +49,17 @@ class Utilities(unittest.TestCase):
self.assertEqual('x007_bond', key2param('007-bond')) self.assertEqual('x007_bond', key2param('007-bond'))
class DiscoveryErrors(unittest.TestCase):
def test_failed_to_parse_discovery_json(self):
self.http = HttpMock(datafile('malformed.json'), {'status': '200'})
try:
buzz = build('buzz', 'v1', self.http)
self.fail("should have raised an exception over malformed JSON.")
except HttpError, e:
self.assertEqual(e.content, "{\n")
class Discovery(unittest.TestCase): class Discovery(unittest.TestCase):
def test_method_error_checking(self): def test_method_error_checking(self):