Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import sys 

2 

3from pyramid.compat import reraise 

4from pyramid.httpexceptions import HTTPNotFound 

5 

6 

7def _error_handler(request, exc): 

8 # NOTE: we do not need to delete exc_info because this function 

9 # should never be in the call stack of the exception 

10 exc_info = sys.exc_info() 

11 

12 try: 

13 response = request.invoke_exception_view(exc_info) 

14 except HTTPNotFound: 

15 # re-raise the original exception as no exception views were 

16 # able to handle the error 

17 reraise(*exc_info) 

18 

19 return response 

20 

21 

22def excview_tween_factory(handler, registry): 

23 """ A :term:`tween` factory which produces a tween that catches an 

24 exception raised by downstream tweens (or the main Pyramid request 

25 handler) and, if possible, converts it into a Response using an 

26 :term:`exception view`. 

27 

28 .. versionchanged:: 1.9 

29 The ``request.response`` will be remain unchanged even if the tween 

30 handles an exception. Previously it was deleted after handling an 

31 exception. 

32 

33 Also, ``request.exception`` and ``request.exc_info`` are only set if 

34 the tween handles an exception and returns a response otherwise they 

35 are left at their original values. 

36 

37 """ 

38 

39 def excview_tween(request): 

40 try: 

41 response = handler(request) 

42 except Exception as exc: 

43 response = _error_handler(request, exc) 

44 return response 

45 

46 return excview_tween 

47 

48 

49MAIN = 'MAIN' 

50INGRESS = 'INGRESS' 

51EXCVIEW = 'pyramid.tweens.excview_tween_factory'