Browse Source

clearer exception with chaining

Guénaël Muller 6 years ago
parent
commit
11fa78cdda
1 changed files with 18 additions and 17 deletions
  1. 18 17
      tracim/lib/utils/request.py

+ 18 - 17
tracim/lib/utils/request.py View File

225
                 workspace=workspace,
225
                 workspace=workspace,
226
                 parent=content,
226
                 parent=content,
227
             )
227
             )
228
-        except JSONDecodeError:
229
-            raise ContentNotFound('Bad json body')
230
-        except NoResultFound:
228
+        except JSONDecodeError as exc:
229
+            raise ContentNotFound('Invalid JSON content') from exc
230
+        except NoResultFound as exc:
231
             raise ContentNotFound(
231
             raise ContentNotFound(
232
                 'Comment {} does not exist '
232
                 'Comment {} does not exist '
233
                 'or is not visible for this user'.format(comment_id)
233
                 'or is not visible for this user'.format(comment_id)
234
-            )
234
+            ) from exc
235
         return comment
235
         return comment
236
 
236
 
237
     def _get_current_content(
237
     def _get_current_content(
258
                 config=request.registry.settings['CFG']
258
                 config=request.registry.settings['CFG']
259
             )
259
             )
260
             content = api.get_one(content_id=content_id, workspace=workspace, content_type=ContentType.Any)  # nopep8
260
             content = api.get_one(content_id=content_id, workspace=workspace, content_type=ContentType.Any)  # nopep8
261
-        except JSONDecodeError:
262
-            raise ContentNotFound('Bad json body')
263
-        except NoResultFound:
261
+        except JSONDecodeError as exc:
262
+            raise ContentNotFound('Invalid JSON content') from exc
263
+        except NoResultFound as exc:
264
             raise ContentNotFound(
264
             raise ContentNotFound(
265
                 'Content {} does not exist '
265
                 'Content {} does not exist '
266
                 'or is not visible for this user'.format(content_id)
266
                 'or is not visible for this user'.format(content_id)
267
-            )
267
+            ) from exc
268
         return content
268
         return content
269
 
269
 
270
     def _get_candidate_user(
270
     def _get_candidate_user(
278
         """
278
         """
279
         app_config = request.registry.settings['CFG']
279
         app_config = request.registry.settings['CFG']
280
         uapi = UserApi(None, session=request.dbsession, config=app_config)
280
         uapi = UserApi(None, session=request.dbsession, config=app_config)
281
-
281
+        login = ''
282
         try:
282
         try:
283
             login = None
283
             login = None
284
             if 'user_id' in request.matchdict:
284
             if 'user_id' in request.matchdict:
301
         """
301
         """
302
         app_config = request.registry.settings['CFG']
302
         app_config = request.registry.settings['CFG']
303
         uapi = UserApi(None, session=request.dbsession, config=app_config)
303
         uapi = UserApi(None, session=request.dbsession, config=app_config)
304
+        login = ''
304
         try:
305
         try:
305
             login = request.authenticated_userid
306
             login = request.authenticated_userid
306
             if not login:
307
             if not login:
333
                 config=request.registry.settings['CFG']
334
                 config=request.registry.settings['CFG']
334
             )
335
             )
335
             workspace = wapi.get_one(workspace_id)
336
             workspace = wapi.get_one(workspace_id)
336
-        except JSONDecodeError:
337
-            raise WorkspaceNotFound('Bad json body')
338
-        except NoResultFound:
337
+        except JSONDecodeError as exc:
338
+            raise WorkspaceNotFound('Invalid JSON content') from exc
339
+        except NoResultFound as exc:
339
             raise WorkspaceNotFound(
340
             raise WorkspaceNotFound(
340
                 'Workspace {} does not exist '
341
                 'Workspace {} does not exist '
341
                 'or is not visible for this user'.format(workspace_id)
342
                 'or is not visible for this user'.format(workspace_id)
342
-            )
343
+            ) from exc
343
         return workspace
344
         return workspace
344
 
345
 
345
     def _get_candidate_workspace(
346
     def _get_candidate_workspace(
365
                 config=request.registry.settings['CFG']
366
                 config=request.registry.settings['CFG']
366
             )
367
             )
367
             workspace = wapi.get_one(workspace_id)
368
             workspace = wapi.get_one(workspace_id)
368
-        except JSONDecodeError:
369
-            raise WorkspaceNotFound('Bad json body')
370
-        except NoResultFound:
369
+        except JSONDecodeError as exc:
370
+            raise WorkspaceNotFound('Invalid JSON content') from exc
371
+        except NoResultFound as exc:
371
             raise WorkspaceNotFound(
372
             raise WorkspaceNotFound(
372
                 'Workspace {} does not exist '
373
                 'Workspace {} does not exist '
373
                 'or is not visible for this user'.format(workspace_id)
374
                 'or is not visible for this user'.format(workspace_id)
374
-            )
375
+            ) from exc
375
         return workspace
376
         return workspace