Browse Source

fix logger and catch request exception

Guénaël Muller 7 years ago
parent
commit
9c0a10ec1c
1 changed files with 29 additions and 4 deletions
  1. 29 4
      tracim/tracim/lib/email_fetcher.py

+ 29 - 4
tracim/tracim/lib/email_fetcher.py View File

@@ -16,9 +16,9 @@ import requests
16 16
 from bs4 import BeautifulSoup
17 17
 from email_reply_parser import EmailReplyParser
18 18
 
19
+from tracim.lib.base import logger
19 20
 from tracim.controllers.events import VALID_TOKEN_VALUE
20 21
 
21
-
22 22
 TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
23 23
 BS_HTML_BODY_PARSE_CONFIG = {
24 24
     'tag_blacklist': ["script", "style", "blockquote"],
@@ -258,6 +258,7 @@ class MailFetcher(object):
258 258
             logger.debug(self, log.format(str(rv)))
259 259
 
260 260
     def _notify_tracim(self) -> None:
261
+        unsended_mail = []
261 262
         while self._mails:
262 263
             mail = self._mails.pop()
263 264
             msg = {"token": VALID_TOKEN_VALUE,
@@ -266,6 +267,30 @@ class MailFetcher(object):
266 267
                    "payload": {
267 268
                        "content": mail.get_body(),
268 269
                    }}
269
-            # FIXME - G.M - 2017-11-15 - Catch exception from http request
270
-            requests.post(self.endpoint, json=msg)
271
-            pass
270
+            try:
271
+                r = requests.post(self.endpoint, json=msg)
272
+                response = r.json()
273
+                if not 'status' in response:
274
+                    log = 'bad response: {}'
275
+                    logger.error(self, log.format(str(response)))
276
+                else:
277
+                    if response['status'] == 'ok':
278
+                        pass
279
+                    elif response['status'] == 'error' and 'error' in response:
280
+                        log = 'error with email: {}'
281
+                        logger.error(self, log.format(str(response['error'])))
282
+                    else:
283
+                        log = 'Unknown error with email'
284
+            # TODO - G.M - Verify exception correctly works
285
+            except requests.exceptions.Timeout:
286
+                log = 'Timeout error to transmit fetched mail to tracim : {}'
287
+                logger.error(self, log.format(str(e)))
288
+                unsended_mail.append(mail)
289
+                break
290
+            except requests.exceptions.RequestException as e:
291
+                log = 'Fail to transmit fetched mail to tracim : {}'
292
+                logger.error(self, log.format(str(e)))
293
+                break
294
+        # FIXME - G.M - 2017-11-17 Avoid too short-timed infinite retry ?
295
+        # retry later to send those mail
296
+        self._mails = unsended_mail