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