summaryrefslogtreecommitdiff
path: root/docs/devel_guide_src/errorHandling.tex
blob: 02079d5c5d63d41e1df550e9ac2e8f99f246e1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
\section{Directives: Error Handling}
\label{errorHandling}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\#try and \#raise}
\label{errorHandling.try}

The template:
\begin{verbatim}
#import traceback
#try
#raise RuntimeError
#except RuntimeError
A runtime error occurred.
#end try

#try
#raise RuntimeError("Hahaha!")
#except RuntimeError
#echo $sys.exc_info()[1]
#end try

#try
#echo 1/0
#except ZeroDivisionError
You can't divide by zero, idiot!
#end try
\end{verbatim}

The output:
\begin{verbatim}
A runtime error occurred.

Hahaha!

You can't divide by zero, idiot!
\end{verbatim}

The generated code:
\begin{verbatim}
try:
    raise RuntimeError
except RuntimeError:
    write('A runtime error occurred.\n')
write('\n')
try:
    raise RuntimeError("Hahaha!")
except RuntimeError:
    write(filter(VFN(sys,"exc_info",0)()[1]))
    write('\n')
write('\n')
try:
    write(filter(1/0))
    write('\n')
except ZeroDivisionError:
    write("You can't divide by zero, idiot!\n")
\end{verbatim}

\code{\#finally} works just like in Python.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\#assert}
\label{errorHandling.assert}

The template:
\begin{verbatim}
#assert False, "You lose, buster!"
\end{verbatim}

The output:
\begin{verbatim}
Traceback (most recent call last):
  File "x.py", line 117, in ?
    x().runAsMainProgram()
  File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
Template.py", line 331, in runAsMainProgram
    CmdLineIface(templateObj=self).run()
  File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
TemplateCmdLineIface.py", line 59, in run
    print self._template
  File "x.py", line 91, in respond
    assert False, "You lose, buster!"
AssertionError: You lose, buster!
\end{verbatim}

The generated code:
\begin{verbatim}
assert False, "You lose, buster!"
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\#errorCatcher}
\label{errorHandling.errorCatcher}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{No error catcher}
\label{errorHandling.errorCatcher.no}

The template:
\begin{verbatim}
$noValue
\end{verbatim}

The output:
\begin{verbatim}

Traceback (most recent call last):
  File "x.py", line 118, in ?
    x().runAsMainProgram()
  File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
Template.py", line 331, in runAsMainProgram
    CmdLineIface(templateObj=self).run()
  File "/local/opt/Python/lib/python2.2/site-packages/Webware/Cheetah/
TemplateCmdLineIface.py", line 59, in run
    print self._template
  File "x.py", line 91, in respond
    write(filter(VFS(SL,"noValue",1))) # generated from '$noValue' at line 
1, col 1.
NameMapper.NotFound: noValue
\end{verbatim}

The generated code:
\begin{verbatim}
write(filter(VFS(SL,"noValue",1))) # generated from '$noValue' at line 1, 
    # col 1.
write('\n')
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Echo and BigEcho}
\label{errorHandling.errorCatcher.echo}

The template:
\begin{verbatim}
#errorCatcher Echo
$noValue
#errorCatcher BigEcho
$noValue
\end{verbatim}

The output:
\begin{verbatim}
$noValue
===============<$noValue could not be found>===============
\end{verbatim}

The generated code:
\begin{verbatim}
if self._errorCatchers.has_key("Echo"):
    self._errorCatcher = self._errorCatchers["Echo"]
else:
    self._errorCatcher = self._errorCatchers["Echo"] = ErrorCatchers.Echo(self)
write(filter(self.__errorCatcher1(localsDict=locals()))) 
    # generated from '$noValue' at line 2, col 1.
write('\n')
if self._errorCatchers.has_key("BigEcho"):
    self._errorCatcher = self._errorCatchers["BigEcho"]
else:
    self._errorCatcher = self._errorCatchers["BigEcho"] = \
        ErrorCatchers.BigEcho(self)
write(filter(self.__errorCatcher1(localsDict=locals()))) 
        # generated from '$noValue' at line 4, col 1.
write('\n')
\end{verbatim}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{ListErrors}
\label{errorHandling.errorCatcher.listErrors}

The template:
\begin{verbatim}
#import pprint
#errorCatcher ListErrors
$noValue
$anotherMissingValue.really
$pprint.pformat($errorCatcher.listErrors)
## This is really self.errorCatcher().listErrors()
\end{verbatim}

The output:
\begin{verbatim}
$noValue
$anotherMissingValue.really
[{'code': 'VFS(SL,"noValue",1)',
  'exc_val': <NameMapper.NotFound instance at 0x8170ecc>,
  'lineCol': (3, 1),
  'rawCode': '$noValue',
  'time': 'Wed May 15 00:38:23 2002'},
 {'code': 'VFS(SL,"anotherMissingValue.really",1)',
  'exc_val': <NameMapper.NotFound instance at 0x816d0fc>,
  'lineCol': (4, 1),
  'rawCode': '$anotherMissingValue.really',
  'time': 'Wed May 15 00:38:23 2002'}]
\end{verbatim}

The generated import:
\begin{verbatim}
import pprint
\end{verbatim}

Then in the generated class, we have our familiar \code{.respond} method
and several new methods:
\begin{verbatim}
def __errorCatcher1(self, localsDict={}):
    """
    Generated from $noValue at line, col (3, 1).
    """

    try:
        return eval('''VFS(SL,"noValue",1)''', globals(), localsDict)
    except self._errorCatcher.exceptions(), e:
        return self._errorCatcher.warn(exc_val=e, code= 'VFS(SL,"noValue",1)' ,
	    rawCode= '$noValue' , lineCol=(3, 1))

def __errorCatcher2(self, localsDict={}):
    """
    Generated from $anotherMissingValue.really at line, col (4, 1).
    """

    try:
        return eval('''VFS(SL,"anotherMissingValue.really",1)''', globals(), 
	    localsDict)
    except self._errorCatcher.exceptions(), e:
        return self._errorCatcher.warn(exc_val=e, 
	    code= 'VFS(SL,"anotherMissingValue.really",1)' , 
	    rawCode= '$anotherMissingValue.really' , lineCol=(4, 1))

def __errorCatcher3(self, localsDict={}):
    """
    Generated from $pprint.pformat($errorCatcher.listErrors) at line, col 
    (5, 1).
    """

    try:
        return eval('''VFN(pprint,"pformat",0)(VFS(SL,
	    "errorCatcher.listErrors",1))''', globals(), localsDict)
    except self._errorCatcher.exceptions(), e:
        return self._errorCatcher.warn(exc_val=e, code= 
	    'VFN(pprint,"pformat",0)(VFS(SL,"errorCatcher.listErrors",1))' , 
	    rawCode= '$pprint.pformat($errorCatcher.listErrors)' , 
	    lineCol=(5, 1))
\end{verbatim}
\begin{verbatim}
def respond(self,
        trans=None,
        dummyTrans=False,
        VFS=valueFromSearchList,
        VFN=valueForName,
        getmtime=getmtime,
        currentTime=time.time):


    """
    This is the main method generated by Cheetah
    """

    if not trans:
        trans = DummyTransaction()
        dummyTrans = True
    write = trans.response().write
    SL = self._searchList
    filter = self._currentFilter
    globalSetVars = self._globalSetVars
    
    ########################################
    ## START - generated method body
    
    if exists(self._filePath) and getmtime(self._filePath) > self._fileMtime:
        self.compile(file=self._filePath)
        write(getattr(self, self._mainCheetahMethod_for_x)(trans=trans))
        if dummyTrans:
            return trans.response().getvalue()
        else:
            return ""
    if self._errorCatchers.has_key("ListErrors"):
        self._errorCatcher = self._errorCatchers["ListErrors"]
    else:
        self._errorCatcher = self._errorCatchers["ListErrors"] = \
	    ErrorCatchers.ListErrors(self)
    write(filter(self.__errorCatcher1(localsDict=locals()))) 
        # generated from '$noValue' at line 3, col 1.
    write('\n')
    write(filter(self.__errorCatcher2(localsDict=locals()))) 
        # generated from '$anotherMissingValue.really' at line 4, col 1.
    write('\n')
    write(filter(self.__errorCatcher3(localsDict=locals()))) 
        # generated from '$pprint.pformat($errorCatcher.listErrors)' at line 
	# 5, col 1.
    write('\n')
    #  This is really self.errorCatcher().listErrors()
    
    ########################################
    ## END - generated method body
    
    if dummyTrans:
        return trans.response().getvalue()
    else:
        return ""
\end{verbatim}

So whenever an error catcher is active, each placeholder gets wrapped in its
own method.  No wonder error catchers slow down the system!


% Local Variables:
% TeX-master: "devel_guide"
% End: