Skip to content

Commit

Permalink
Request: add "silent" option, returning None instead of raising reque…
Browse files Browse the repository at this point in the history
…sts exceptions.

Silent option does log to error_log.
  • Loading branch information
jone committed Apr 17, 2012
1 parent 443c2c9 commit cf2df3b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ftw/bridge/client/request.py
Expand Up @@ -6,11 +6,13 @@
from ftw.bridge.client.interfaces import IBridgeRequest
from ftw.bridge.client.interfaces import PORTAL_URL_PLACEHOLDER
from ftw.bridge.client.utils import json
from requests.exceptions import RequestException
from requests.models import Response
from zope.app.component.hooks import getSite
from zope.component import getUtility
from zope.interface import implements
import requests
import sys
import types
import urlparse

Expand All @@ -37,7 +39,8 @@ def replace_placeholder_in_data(data, public_url):
class BridgeRequest(object):
implements(IBridgeRequest)

def __call__(self, target, path, method='GET', headers=None, **kwargs):
def __call__(self, target, path, method='GET', headers=None,
silent=False, **kwargs):
"""Makes a request to a remote client.
"""
config = getUtility(IBridgeConfig)
Expand All @@ -50,7 +53,14 @@ def __call__(self, target, path, method='GET', headers=None, **kwargs):
request_args = kwargs.copy()
request_args['headers'] = self._get_headers(config, headers)

response = self._do_request(method, url, **request_args)
try:
response = self._do_request(method, url, **request_args)
except RequestException:
if silent:
getSite().error_log.raising(sys.exc_info())
return None
else:
raise

if int(response.status_code) == 503:
raise MaintenanceError()
Expand Down
33 changes: 33 additions & 0 deletions ftw/bridge/client/tests/test_request.py
Expand Up @@ -9,6 +9,7 @@
from ftw.bridge.client.testing import BRIDGE_CONFIG_LAYER
from ftw.testing import MockTestCase
from mocker import ANY, ARGS, KWARGS
from requests.exceptions import ConnectionError
from requests.models import Response
from unittest2 import TestCase
from zope.app.component.hooks import setSite
Expand Down Expand Up @@ -269,3 +270,35 @@ def conflict_error_view_method():

with self.assertRaises(ConflictError):
utility('current-client', 'baz/@@view?foo=bar')

def test_silent_error_is_logged(self):
def raise_connection_error(*args, **kwargs):
raise ConnectionError()

self.expect(self.requests.request(ANY, ANY, KWARGS)).call(
raise_connection_error)

site = self.stub()
self.expect(site.getSiteManager()).call(getGlobalSiteManager)
self.expect(site.error_log.raising(ANY))

self.replay()

utility = getUtility(IBridgeRequest)
setSite(site)

self.assertEqual(
utility('target-client', 'path/to/@@something', silent=True),
None)

def test_nonsilent_error_is_reraised(self):
def raise_connection_error(*args, **kwargs):
raise ConnectionError()

self.expect(self.requests.request(ANY, ANY, KWARGS)).call(
raise_connection_error)

self.replay()
utility = getUtility(IBridgeRequest)
with self.assertRaises(ConnectionError):
utility('target-client', 'path/to/@@something')

0 comments on commit cf2df3b

Please sign in to comment.