Commit 0f14fe27 authored by Łukasz Nowak's avatar Łukasz Nowak

http: handle RequestException

It's kind of requests catch-all excpetion, so print information in this case
in order to debug it better.
parent 48257913
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
import requests import requests
import sys
import traceback
from urllib.parse import urlparse, urlunsplit from urllib.parse import urlparse, urlunsplit
from forcediphttpsadapter.adapters import ForcedIPHTTPSAdapter from forcediphttpsadapter.adapters import ForcedIPHTTPSAdapter
from peewee import fn from peewee import fn
...@@ -84,6 +86,10 @@ def request(url, timeout=TIMEOUT, headers=None, session=requests, version=0): ...@@ -84,6 +86,10 @@ def request(url, timeout=TIMEOUT, headers=None, session=requests, version=0):
except requests.exceptions.TooManyRedirects: except requests.exceptions.TooManyRedirects:
response = requests.models.Response() response = requests.models.Response()
response.status_code = 520 response.status_code = 520
except requests.exceptions.RequestException:
response = requests.models.Response()
response.status_code = 000
print(traceback.print_exception(*(sys.exc_info())))
return response return response
......
...@@ -178,6 +178,19 @@ class SurykatkaHttpTestCase(unittest.TestCase): ...@@ -178,6 +178,19 @@ class SurykatkaHttpTestCase(unittest.TestCase):
assert mock_request.call_count == 1 assert mock_request.call_count == 1
assert response.status_code == 524, response.status_code assert response.status_code == 524, response.status_code
def test_request_RequestException(self):
url_to_proxy = "http://example.org/"
httpretty.register_uri(httpretty.GET, url_to_proxy)
with mock.patch("surykatka.http.requests.request") as mock_request:
def sideEffect(*args, **kw):
raise surykatka.http.requests.exceptions.RequestException()
mock_request.side_effect = sideEffect
response = request(url_to_proxy)
assert mock_request.call_count == 1
assert response.status_code == 000, response.status_code
def test_request_tooManyRedirect(self): def test_request_tooManyRedirect(self):
url_to_proxy = "http://example.org/" url_to_proxy = "http://example.org/"
httpretty.register_uri(httpretty.GET, url_to_proxy) httpretty.register_uri(httpretty.GET, url_to_proxy)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment