Permalink
Browse files

Request: add "silent" option, returning None instead of raising reque…

…sts exceptions.

Silent option does log to error_log.
  • Loading branch information...
1 parent 443c2c9 commit cf2df3b2853fef441e1142bffaa3c87757881adf @jone jone committed Apr 17, 2012
Showing with 45 additions and 2 deletions.
  1. +12 −2 ftw/bridge/client/request.py
  2. +33 −0 ftw/bridge/client/tests/test_request.py
@@ -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
@@ -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)
@@ -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()
@@ -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
@@ -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.