Debian Codegraph @

docs/dev_guide/errorHandling.rst

raw
1 Directives: Error Handling
2 ==========================
3
4
5 #try and #raise
6 ---------------
7
8
9 The template:
10
11 ::
12
13 #import traceback
14 #try
15 #raise RuntimeError
16 #except RuntimeError
17 A runtime error occurred.
18 #end try
19
20 #try
21 #raise RuntimeError("Hahaha!")
22 #except RuntimeError
23 #echo $sys.exc_info()[1]
24 #end try
25
26 #try
27 #echo 1/0
28 #except ZeroDivisionError
29 You can't divide by zero, idiot!
30 #end try
31
32 The output:
33
34 ::
35
36 A runtime error occurred.
37
38 Hahaha!
39
40 You can't divide by zero, idiot!
41
42 The generated code:
43
44 ::
45
46 try:
47 raise RuntimeError
48 except RuntimeError:
49 write('A runtime error occurred.\n')
50 write('\n')
51 try:
52 raise RuntimeError("Hahaha!")
53 except RuntimeError:
54 write(filter(VFN(sys,"exc_info",0)()[1]))
55 write('\n')
56 write('\n')
57 try:
58 write(filter(1/0))
59 write('\n')
60 except ZeroDivisionError:
61 write("You can't divide by zero, idiot!\n")
62
63 {#finally} works just like in Python.
64
65 #assert
66 -------
67
68
69 The template:
70
71 ::
72
73 #assert False, "You lose, buster!"
74
75 The output:
76
77 ::
78
79 Traceback (most recent call last):
80 File "x.py", line 117, in ?
81 x().runAsMainProgram()
82 File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
83 Template.py", line 331, in runAsMainProgram
84 CmdLineIface(templateObj=self).run()
85 File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
86 TemplateCmdLineIface.py", line 59, in run
87 print self._template
88 File "x.py", line 91, in respond
89 assert False, "You lose, buster!"
90 AssertionError: You lose, buster!
91
92 The generated code:
93
94 ::
95
96 assert False, "You lose, buster!"
97
98 #errorCatcher
99 -------------
100
101
102 No error catcher
103 ~~~~~~~~~~~~~~~~
104
105
106 The template:
107
108 ::
109
110 $noValue
111
112 The output:
113
114 ::
115
116 Traceback (most recent call last):
117 File "x.py", line 118, in ?
118 x().runAsMainProgram()
119 File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
120 Template.py", line 331, in runAsMainProgram
121 CmdLineIface(templateObj=self).run()
122 File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
123 TemplateCmdLineIface.py", line 59, in run
124 print self._template
125 File "x.py", line 91, in respond
126 write(filter(VFS(SL,"noValue",1))) # generated from '$noValue' at line
127 1, col 1.
128 NameMapper.NotFound: noValue
129
130 The generated code:
131
132 ::
133
134 write(filter(VFS(SL,"noValue",1))) # generated from '$noValue' at line 1,
135 # col 1.
136 write('\n')
137
138 Echo and BigEcho
139 ~~~~~~~~~~~~~~~~
140
141
142 The template:
143
144 ::
145
146 #errorCatcher Echo
147 $noValue
148 #errorCatcher BigEcho
149 $noValue
150
151 The output:
152
153 ::
154
155 $noValue
156 ===============<$noValue could not be found>===============
157
158 The generated code:
159
160 ::
161
162 if "Echo" in self._errorCatchers:
163 self._errorCatcher = self._errorCatchers["Echo"]
164 else:
165 self._errorCatcher = self._errorCatchers["Echo"] = ErrorCatchers.Echo(self)
166 write(filter(self.__errorCatcher1(localsDict=locals())))
167 # generated from '$noValue' at line 2, col 1.
168 write('\n')
169 if "BigEcho" in self._errorCatchers:
170 self._errorCatcher = self._errorCatchers["BigEcho"]
171 else:
172 self._errorCatcher = self._errorCatchers["BigEcho"] = \
173 ErrorCatchers.BigEcho(self)
174 write(filter(self.__errorCatcher1(localsDict=locals())))
175 # generated from '$noValue' at line 4, col 1.
176 write('\n')
177
178 ListErrors
179 ~~~~~~~~~~
180
181
182 The template:
183
184 ::
185
186 #import pprint
187 #errorCatcher ListErrors
188 $noValue
189 $anotherMissingValue.really
190 $pprint.pformat($errorCatcher.listErrors)
191 ## This is really self.errorCatcher().listErrors()
192
193 The output:
194
195 ::
196
197 $noValue
198 $anotherMissingValue.really
199 [{'code': 'VFS(SL,"noValue",1)',
200 'exc_val': <NameMapper.NotFound instance at 0x8170ecc>,
201 'lineCol': (3, 1),
202 'rawCode': '$noValue',
203 'time': 'Wed May 15 00:38:23 2002'},
204 {'code': 'VFS(SL,"anotherMissingValue.really",1)',
205 'exc_val': <NameMapper.NotFound instance at 0x816d0fc>,
206 'lineCol': (4, 1),
207 'rawCode': '$anotherMissingValue.really',
208 'time': 'Wed May 15 00:38:23 2002'}]
209
210 The generated import:
211
212 ::
213
214 import pprint
215
216 Then in the generated class, we have our familiar {.respond} method
217 and several new methods:
218
219 ::
220
221 def __errorCatcher1(self, localsDict={}):
222 """
223 Generated from $noValue at line, col (3, 1).
224 """
225
226 try:
227 return eval('''VFS(SL,"noValue",1)''', globals(), localsDict)
228 except self._errorCatcher.exceptions(), e:
229 return self._errorCatcher.warn(exc_val=e, code= 'VFS(SL,"noValue",1)' ,
230 rawCode= '$noValue' , lineCol=(3, 1))
231
232 def __errorCatcher2(self, localsDict={}):
233 """
234 Generated from $anotherMissingValue.really at line, col (4, 1).
235 """
236
237 try:
238 return eval('''VFS(SL,"anotherMissingValue.really",1)''', globals(),
239 localsDict)
240 except self._errorCatcher.exceptions(), e:
241 return self._errorCatcher.warn(exc_val=e,
242 code= 'VFS(SL,"anotherMissingValue.really",1)' ,
243 rawCode= '$anotherMissingValue.really' , lineCol=(4, 1))
244
245 def __errorCatcher3(self, localsDict={}):
246 """
247 Generated from $pprint.pformat($errorCatcher.listErrors) at line, col
248 (5, 1).
249 """
250
251 try:
252 return eval('''VFN(pprint,"pformat",0)(VFS(SL,
253 "errorCatcher.listErrors",1))''', globals(), localsDict)
254 except self._errorCatcher.exceptions(), e:
255 return self._errorCatcher.warn(exc_val=e, code=
256 'VFN(pprint,"pformat",0)(VFS(SL,"errorCatcher.listErrors",1))' ,
257 rawCode= '$pprint.pformat($errorCatcher.listErrors)' ,
258 lineCol=(5, 1))
259
260 ::
261
262 def respond(self,
263 trans=None,
264 dummyTrans=False,
265 VFS=valueFromSearchList,
266 VFN=valueForName,
267 getmtime=getmtime,
268 currentTime=time.time):
269
270
271 """
272 This is the main method generated by Cheetah
273 """
274
275 if not trans:
276 trans = DummyTransaction()
277 dummyTrans = True
278 write = trans.response().write
279 SL = self._searchList
280 filter = self._currentFilter
281 globalSetVars = self._globalSetVars
282
283 ########################################
284 ## START - generated method body
285
286 if exists(self._filePath) and getmtime(self._filePath) > self._fileMtime:
287 self.compile(file=self._filePath)
288 write(getattr(self, self._mainCheetahMethod_for_x)(trans=trans))
289 if dummyTrans:
290 return trans.response().getvalue()
291 else:
292 return ""
293 if "ListErrors" in self._errorCatchers:
294 self._errorCatcher = self._errorCatchers["ListErrors"]
295 else:
296 self._errorCatcher = self._errorCatchers["ListErrors"] = \
297 ErrorCatchers.ListErrors(self)
298 write(filter(self.__errorCatcher1(localsDict=locals())))
299 # generated from '$noValue' at line 3, col 1.
300 write('\n')
301 write(filter(self.__errorCatcher2(localsDict=locals())))
302 # generated from '$anotherMissingValue.really' at line 4, col 1.
303 write('\n')
304 write(filter(self.__errorCatcher3(localsDict=locals())))
305 # generated from '$pprint.pformat($errorCatcher.listErrors)' at line
306 # 5, col 1.
307 write('\n')
308 # This is really self.errorCatcher().listErrors()
309
310 ########################################
311 ## END - generated method body
312
313 if dummyTrans:
314 return trans.response().getvalue()
315 else:
316 return ""
317
318 So whenever an error catcher is active, each placeholder gets
319 wrapped in its own method. No wonder error catchers slow down the
320 system!
321
322