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
 from bs4 import BeautifulSoup
16
 from bs4 import BeautifulSoup
17
 from email_reply_parser import EmailReplyParser
17
 from email_reply_parser import EmailReplyParser
18
 
18
 
19
+from tracim.lib.base import logger
19
 from tracim.controllers.events import VALID_TOKEN_VALUE
20
 from tracim.controllers.events import VALID_TOKEN_VALUE
20
 
21
 
21
-
22
 TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
22
 TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
23
 BS_HTML_BODY_PARSE_CONFIG = {
23
 BS_HTML_BODY_PARSE_CONFIG = {
24
     'tag_blacklist': ["script", "style", "blockquote"],
24
     'tag_blacklist': ["script", "style", "blockquote"],
258
             logger.debug(self, log.format(str(rv)))
258
             logger.debug(self, log.format(str(rv)))
259
 
259
 
260
     def _notify_tracim(self) -> None:
260
     def _notify_tracim(self) -> None:
261
+        unsended_mail = []
261
         while self._mails:
262
         while self._mails:
262
             mail = self._mails.pop()
263
             mail = self._mails.pop()
263
             msg = {"token": VALID_TOKEN_VALUE,
264
             msg = {"token": VALID_TOKEN_VALUE,
266
                    "payload": {
267
                    "payload": {
267
                        "content": mail.get_body(),
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