[Zope] Trapping zope exceptions in python script

Jonathan dev101 at magma.ca
Wed Dec 14 14:34:05 EST 2005


I am trying to catch a 'BadRequest' exception in the following python script 
excerpt:

for item in itemList:
   try:
      context.afolder.manage_delObjects([item])
   except BadRequest:
      continue


The rationale behind this is that itemList may contain entries that no 
longer exist in 'afolder', but I would like to delete whatever does exist. 
'BadRequest' is not a python built-in exception, so whenever the above 
except clause is invoked it actually causes another error:

Traceback (innermost last):
  Module ZPublisher.Publish, line 98, in publish
  Module ZPublisher.mapply, line 88, in mapply
  Module ZPublisher.Publish, line 39, in call_object
  Module OFS.DTMLMethod, line 126, in __call__
  Module DocumentTemplate.DT_String, line 474, in __call__
  Module DocumentTemplate.DT_In, line 705, in renderwob
  Module DocumentTemplate.DT_Util, line 201, in eval
   - __traceback_info__: _
  Module <string>, line 2, in f
  Module Shared.DC.Scripts.Bindings, line 252, in __call__
  Module Shared.DC.Scripts.Bindings, line 283, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 315, in _exec
  Module Script (Python), line 25, in subDeleteAd
   - <PythonScript at /fas/subDeleteAd>
   - Line 25
NameError: global name 'BadRequest' is not defined


I can work around this problem by checking to see if the item exists in 
'afolder' before I try to delete it, but I was curious as to how you go 
about trapping a 'BadRequest' error in a python script?

Thanks,

Jonathan 




More information about the Zope mailing list