Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Handle RequestException as Service Unavailable errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Oct 2, 2015
1 parent c39dabb commit 0381719
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 15 additions & 1 deletion syncto/tests/test_functional.py
Expand Up @@ -4,7 +4,7 @@

from cliquet.errors import ERRORS
from cliquet.tests.support import FormattedErrorMixin
from requests.exceptions import HTTPError
from requests.exceptions import HTTPError, ConnectionError
from syncto import AUTHORIZATION_HEADER, CLIENT_STATE_HEADER
from syncto import main as testapp

Expand Down Expand Up @@ -114,6 +114,20 @@ def test_error_with_syncclient_server_raise_a_503(self):
self.assertFormattedError(
resp, 503, ERRORS.BACKEND, "Service Unavailable", "retry later")

def test_error_with_syncclient_server_request(self):
headers = self.headers.copy()
headers['Authorization'] = "BrowserID valid-browser-id-assertion"
headers['X-Client-State'] = "ValidClientState"
with mock.patch("syncto.authentication.TokenserverClient",
side_effect=ConnectionError):
resp = self.app.get(COLLECTION_URL, headers=headers, status=503)

error_msg = ("Unable to reach the service. Check your "
"internet connection or firewall configuration.")

self.assertFormattedError(
resp, 503, ERRORS.BACKEND, "Service Unavailable", error_msg)


class BaseViewTest(BaseWebTest, unittest.TestCase):
patch_authent_for = 'record'
Expand Down
22 changes: 19 additions & 3 deletions syncto/views/errors.py
@@ -1,7 +1,7 @@
from pyramid import httpexceptions
from pyramid.security import NO_PERMISSION_REQUIRED, forget
from pyramid.view import view_config
from requests.exceptions import HTTPError
from requests.exceptions import HTTPError, RequestException

from cliquet import logger
from cliquet.errors import http_error, ERRORS
Expand All @@ -12,7 +12,7 @@


@view_config(context=HTTPError, permission=NO_PERMISSION_REQUIRED)
def error(context, request):
def response_error(context, request):
"""Catch server errors and trace them."""
message = '%s %s: %s' % (context.response.status_code,
context.response.reason,
Expand Down Expand Up @@ -42,9 +42,25 @@ def error(context, request):
errno=ERRORS.INVALID_RESOURCE_ID,
message=message)
else:
response = service_unavailable(context, request)
response = service_unavailable(
httpexceptions.HTTPServiceUnavailable(),
request)

request.response = response
export_headers(context.response, request)

return reapply_cors(request, response)


@view_config(context=RequestException, permission=NO_PERMISSION_REQUIRED)
def request_error(context, request):
"""Catch requests errors when issuing a request."""
logger.error(context, exc_info=True)

error_msg = ("Unable to reach the service. "
"Check your internet connection or firewall configuration.")
response = http_error(httpexceptions.HTTPServiceUnavailable(),
errno=ERRORS.BACKEND,
message=error_msg)

return service_unavailable(response, request)

0 comments on commit 0381719

Please sign in to comment.