]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Log http requests exceptions
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 4 Apr 2018 10:02:50 +0000 (12:02 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 4 Apr 2018 10:50:34 +0000 (12:50 +0200)
ccxt_wrapper.py
store.py
test.py

index 260d49dccbe2a2221b79fe8c36be70d940a24ab3..bedf84b47dd3d908ace71fefde768e7cab693ea2 100644 (file)
@@ -3,6 +3,8 @@ import decimal
 import time
 from retry.api import retry_call
 import re
+from requests.exceptions import RequestException
+from ssl import SSLError
 
 def _cw_exchange_sum(self, *args):
     return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))])
@@ -45,10 +47,16 @@ class poloniexE(poloniex):
         self.session._parent = self
 
         def request_wrap(self, *args, **kwargs):
-            r = self.origin_request(*args, **kwargs)
-            self._parent._market.report.log_http_request(args[0],
-                    args[1], kwargs["data"], kwargs["headers"], r)
-            return r
+            try:
+                r = self.origin_request(*args, **kwargs)
+                self._parent._market.report.log_http_request(args[0],
+                        args[1], kwargs["data"], kwargs["headers"], r)
+                return r
+            except (SSLError, RequestException) as e:
+                self._parent._market.report.log_http_request(args[0],
+                        args[1], kwargs["data"], kwargs["headers"], e)
+                raise e
+
         self.session.request = request_wrap.__get__(self.session,
                 self.session.__class__)
 
index 2b5c18a1a1a65eb241af2677d322195a5202d18d..3f3718f4f8c0bff4e253a0a090e5820bd8065726 100644 (file)
--- a/store.py
+++ b/store.py
@@ -176,15 +176,28 @@ class ReportStore:
             })
 
     def log_http_request(self, method, url, body, headers, response):
-        self.add_log({
-            "type": "http_request",
-            "method": method,
-            "url": url,
-            "body": body,
-            "headers": headers,
-            "status": response.status_code,
-            "response": response.text
-            })
+        if isinstance(response, Exception):
+            self.add_log({
+                "type": "http_request",
+                "method": method,
+                "url": url,
+                "body": body,
+                "headers": headers,
+                "status": -1,
+                "response": None,
+                "error": response.__class__.__name__,
+                "error_message": str(response),
+                })
+        else:
+            self.add_log({
+                "type": "http_request",
+                "method": method,
+                "url": url,
+                "body": body,
+                "headers": headers,
+                "status": response.status_code,
+                "response": response.text
+                })
 
     def log_error(self, action, message=None, exception=None):
         self.print_log("[Error] {}".format(action))
diff --git a/test.py b/test.py
index 29403d46c6e48d53b15d61c4e5f9433594ccd329..bf679bfc8c2879f507bdfee64b57c09705619845 100644 (file)
--- a/test.py
+++ b/test.py
@@ -71,7 +71,8 @@ class poloniexETest(unittest.TestCase):
         super().tearDown()
 
     def test__init(self):
-        with mock.patch("market.ccxt.poloniexE.session") as session:
+        with self.subTest("Nominal case"), \
+                mock.patch("market.ccxt.poloniexE.session") as session:
             session.request.return_value = "response"
             ccxt = market.ccxt.poloniexE()
             ccxt._market = mock.Mock
@@ -82,6 +83,21 @@ class poloniexETest(unittest.TestCase):
             ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
                     'headers', 'response')
 
+        with self.subTest("Raising"),\
+                mock.patch("market.ccxt.poloniexE.session") as session:
+            session.request.side_effect = market.ccxt.RequestException("Boo")
+
+            ccxt = market.ccxt.poloniexE()
+            ccxt._market = mock.Mock
+            ccxt._market.report = mock.Mock()
+
+            with self.assertRaises(market.ccxt.RequestException, msg="Boo") as cm:
+                ccxt.session.request("GET", "URL", data="data",
+                        headers="headers")
+            ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
+                    'headers', cm.exception)
+
+
     def test_nanoseconds(self):
         with mock.patch.object(market.ccxt.time, "time") as time:
             time.return_value = 123456.7890123456
@@ -4349,6 +4365,21 @@ class ReportStoreTest(WebMockTestCase):
             'response': 'Hey'
             })
 
+        add_log.reset_mock()
+        report_store.log_http_request("method", "url", "body",
+                "headers", ValueError("Foo"))
+        add_log.assert_called_once_with({
+            'type': 'http_request',
+            'method': 'method',
+            'url': 'url',
+            'body': 'body',
+            'headers': 'headers',
+            'status': -1,
+            'response': None,
+            'error': 'ValueError',
+            'error_message': 'Foo',
+            })
+
     @mock.patch.object(market.ReportStore, "add_log")
     def test_log_market(self, add_log):
         report_store = market.ReportStore(self.m)