Browse Source

Handle common errors in events mail_reply controller

Guénaël Muller 7 years ago
parent
commit
f5ac92fd19
1 changed files with 19 additions and 6 deletions
  1. 19 6
      tracim/tracim/controllers/events.py

+ 19 - 6
tracim/tracim/controllers/events.py View File

@@ -1,6 +1,8 @@
1 1
 import tg
2 2
 from tg import request
3 3
 from tg import RestController
4
+from sqlalchemy.orm.exc import NoResultFound
5
+
4 6
 from tracim.lib.content import ContentApi
5 7
 from tracim.lib.user import UserApi
6 8
 from tracim.model.data import ContentType
@@ -14,13 +16,24 @@ class EventsRestController(RestController):
14 16
     def post(self):
15 17
         json = request.json_body
16 18
         if 'token' in json and json['token'] == VALID_TOKEN_VALUE:
17
-            # TODO check json content
19
+            if 'user_mail' not in json or 'content_id' not in json:
20
+                return {'status': 'error',
21
+                        'error': 'bad json',}
18 22
             uapi = UserApi(None)
19 23
             # TODO support Empty result error
20
-            user = uapi.get_one_by_email(json['user_mail'])
24
+            try:
25
+                user = uapi.get_one_by_email(json['user_mail'])
26
+            except NoResultFound :
27
+                return {'status': 'error',
28
+                        'error': 'bad user mail',}
21 29
             api = ContentApi(user)
22 30
 
23
-            thread = api.get_one(json['content_id'],content_type=ContentType.Any)
31
+            try:
32
+                thread = api.get_one(json['content_id'],
33
+                                     content_type=ContentType.Any)
34
+            except NoResultFound :
35
+                return {'status': 'error',
36
+                        'error': 'bad content id',}
24 37
             # INFO - G.M - 2017-11-17
25 38
             # When content_id is a sub-elem of a main content like Comment,
26 39
             # Attach the thread to the main content.
@@ -28,10 +41,10 @@ class EventsRestController(RestController):
28 41
                 thread = thread.parent
29 42
             if thread.type == ContentType.Folder:
30 43
                 return {'status': 'error',
31
-                        'error': 'comment for folder not allowed'}
44
+                        'error': 'comment for folder not allowed',}
32 45
             api.create_comment(thread.workspace, thread,
33 46
                                json['payload']['content'], True)
47
+            return {'status': 'ok',}
34 48
         else:
35 49
             return {'status': 'error',
36
-                    'error': 'invalid token'}
37
-        return json
50
+                    'error': 'invalid token',}