Debian Codegraph @

docs/users_guide/errorHandling.rst

raw
1 Error Handling
2 ==============
3
4
5 There are two ways to handle runtime errors (exceptions) in
6 Cheetah. The first is with the Cheetah directives that mirror
7 Python's structured exception handling statements. The second is
8 with Cheetah's {ErrorCatcher} framework. These are described
9 below.
10
11 #try ... #except ... #end try, #finally, and #assert
12 ----------------------------------------------------
13
14
15 Cheetah's exception-handling directives are exact mirrors Python's
16 exception-handling statements. See Python's documentation for
17 details. The following Cheetah code demonstrates their use:
18
19 ::
20
21 #try
22 $mightFail()
23 #except
24 It failed
25 #end try
26
27 #try
28 #assert $x == $y
29 #except AssertionError
30 They're not the same!
31 #end try
32
33 #try
34 #raise ValueError
35 #except ValueError
36 #pass
37 #end try
38
39
40 #try
41 $mightFail()
42 #except ValueError
43 Hey, it raised a ValueError!
44 #except NameMapper.NotFound
45 Hey, it raised a NameMapper.NotFound!
46 #else
47 It didn't raise anything!
48 #end try
49
50 #try
51 $mightFail()
52 #finally
53 $cleanup()
54 #end try
55
56 Like Python, {#except} and {#finally} cannot appear in the same
57 try-block, but can appear in nested try-blocks.
58
59 #errorCatcher and ErrorCatcher objects
60 --------------------------------------
61
62
63 Syntax:
64
65 ::
66
67 #errorCatcher CLASS
68 #errorCatcher $PLACEHOLDER_TO_AN_ERROR_CATCHER_INSTANCE
69
70 {ErrorCatcher} is a debugging tool that catches exceptions that
71 occur inside {$placeholder} tags and provides a customizable
72 warning to the developer. Normally, the first missing namespace
73 value raises a {NameMapper.NotFound} error and halts the filling of
74 the template. This requires the developer to resolve the exceptions
75 in order without seeing the subsequent output. When an
76 {ErrorCatcher} is enabled, the developer can see all the exceptions
77 at once as well as the template output around them.
78
79 The {Cheetah.ErrorCatchers} module defines the base class for
80 ErrorCatchers:
81
82 ::
83
84 class ErrorCatcher:
85 _exceptionsToCatch = (NameMapper.NotFound,)
86
87 def __init__(self, templateObj):
88 pass
89
90 def exceptions(self):
91 return self._exceptionsToCatch
92
93 def warn(self, exc_val, code, rawCode, lineCol):
94 return rawCode
95
96 This ErrorCatcher catches {NameMapper.NotFound} exceptions and
97 leaves the offending placeholder visible in its raw form in the
98 template output. If the following template is executed:
99
100 ::
101
102 #errorCatcher Echo
103 #set $iExist = 'Here I am!'
104 Here's a good placeholder: $iExist
105 Here's bad placeholder: $iDontExist
106
107 the output will be:
108
109 ::
110
111 Here's a good placeholder: Here I am!
112 Here's bad placeholder: $iDontExist
113
114 The base class shown above is also accessible under the alias
115 {Cheetah.ErrorCatchers.Echo}. {Cheetah.ErrorCatchers} also provides
116 a number of specialized subclasses that warn about exceptions in
117 different ways. {Cheetah.ErrorCatchers.BigEcho} will output
118
119 ::
120
121 Here's a good placeholder: Here I am!
122 Here's bad placeholder: ===============<$iDontExist could not be found>===============
123
124 ErrorCatcher has a significant performance impact and is turned off
125 by default. It can also be turned on with the {Template} class'
126 {'errorCatcher'} keyword argument. The value of this argument
127 should either be a string specifying which of the classes in
128 {Cheetah.ErrorCatchers} to use, or a class that subclasses
129 {Cheetah.ErrorCatchers.ErrorCatcher}. The {#errorCatcher} directive
130 can also be used to change the errorCatcher part way through a
131 template.
132
133 {Cheetah.ErrorCatchers.ListErrors} will produce the same output as
134 {Echo} while maintaining a list of the errors that can be retrieved
135 later. To retrieve the list, use the {Template} class'
136 {'errorCatcher'} method to retrieve the errorCatcher and then call
137 its {listErrors} method.
138
139 ErrorCatcher doesn't catch exceptions raised inside directives.
140
141