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 310 311 312 313 314 315 316 317 318 319 320 321 322
|
Directives: Error Handling
==========================
#try and #raise
---------------
The template:
::
#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
The output:
::
A runtime error occurred.
Hahaha!
You can't divide by zero, idiot!
The generated code:
::
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")
{#finally} works just like in Python.
#assert
-------
The template:
::
#assert False, "You lose, buster!"
The output:
::
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!
The generated code:
::
assert False, "You lose, buster!"
#errorCatcher
-------------
No error catcher
~~~~~~~~~~~~~~~~
The template:
::
$noValue
The output:
::
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
The generated code:
::
write(filter(VFS(SL,"noValue",1))) # generated from '$noValue' at line 1,
# col 1.
write('\n')
Echo and BigEcho
~~~~~~~~~~~~~~~~
The template:
::
#errorCatcher Echo
$noValue
#errorCatcher BigEcho
$noValue
The output:
::
$noValue
===============<$noValue could not be found>===============
The generated code:
::
if "Echo" in self._errorCatchers:
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 "BigEcho" in self._errorCatchers:
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')
ListErrors
~~~~~~~~~~
The template:
::
#import pprint
#errorCatcher ListErrors
$noValue
$anotherMissingValue.really
$pprint.pformat($errorCatcher.listErrors)
## This is really self.errorCatcher().listErrors()
The output:
::
$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'}]
The generated import:
::
import pprint
Then in the generated class, we have our familiar {.respond} method
and several new methods:
::
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))
::
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 "ListErrors" in self._errorCatchers:
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 ""
So whenever an error catcher is active, each placeholder gets
wrapped in its own method. No wonder error catchers slow down the
system!
|