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

Accomodate JSON services which do not return JSON payloads for errors. #764

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class JSONConnection(Connection):
API_URL_TEMPLATE = None
"""A template for the URL of a particular API call."""

JSON_ERRORS = True
"""Overridable for services which do not return JSON errors."""

@classmethod
def build_api_url(cls, path, query_params=None,
api_base_url=None, api_version=None):
Expand Down Expand Up @@ -289,7 +292,7 @@ def api_request(self, method, path, query_params=None,
method=method, url=url, data=data, content_type=content_type)

if not 200 <= response.status < 300:
raise make_exception(response, content)
raise make_exception(response, content, use_json=self.JSON_ERRORS)

if content and expect_json:
content_type = response.get('content-type', '')
Expand Down
3 changes: 3 additions & 0 deletions gcloud/pubsub/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ class Connection(base_connection.JSONConnection):

API_URL_TEMPLATE = '{api_base_url}/pubsub/{api_version}{path}'
"""A template for the URL of a particular API call."""

JSON_ERRORS = False
"""Pubsub API does not return JSON payloads for errors ?!?."""
10 changes: 10 additions & 0 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ def test_api_request_w_404(self):
)
self.assertRaises(NotFound, conn.api_request, 'GET', '/')

def test_api_request_w_404_no_JSON_in_payload(self):
from gcloud.exceptions import NotFound
conn = self._makeMockOne()
conn.JSON_ERRORS = False
conn._http = _Http(
{'status': '404', 'content-type': 'text/plain'},
'Not Found'
)
self.assertRaises(NotFound, conn.api_request, 'GET', '/')

def test_api_request_w_500(self):
from gcloud.exceptions import InternalServerError
conn = self._makeMockOne()
Expand Down