[Grok-dev] Exceptions and grok.subscribe

Tony Valderrama tvald at MIT.EDU
Thu Jun 26 13:36:22 EDT 2008


Hi,

Is there a reason Grok doesn't print the traceback on the console when 
it encounters a DuplicationError while adding an application?  I 
recently ran into a frustrating bug which was compounded, in part, by 
this feature in Grok's administration interface.  I had registered 
several setup scripts as event handlers listening for a new application 
to be added, but it boils down to this situation:

@grok.subscribe(BuggyApp,grok.IObjectAddedEvent)
def eventHandler1(app,event):
    print "executed buggy setup script"
    raise zope.exceptions.DuplicationError("silly error that shouldn't 
have happened")

@grok.subscribe(BuggyApp,grok.IObjectAddedEvent)
def eventHandler2(app,event):
    print "executed other setup script"


Now, when you go to the grok admin view and add a BuggyApp, it installs 
the application, sets the status message to indicate that there was an 
error because the name is already in use, and otherwise fails silently 
in the middle of executing the setup scripts - which is really bad when 
you're still trying to find your way around Grok and Zope.

Assuming there isn't any way to fix the silent handling of 
DuplicationErrors, do you have any suggestions on how to compensate for 
this?  The best workaround I've come up with is an additional decorator:

def safehandler(f):
    def wrapper(*args,**kw):
        try:
            f(*args,**kw)
        except zope.exceptions.DuplicationError, e:
            traceback.print_exception(e)
            raise
    return wrapper

@grok.subscribe(BuggyApp, grok.IObjectAddedEvent)
@safehandler
def eventHandler(app,event):
    pass


It's awkward, but since grok.subscribe can only be called on the module 
level, I can't wrap the two decorators together into something like 
@safesubscribe.

Thanks,
Tony Valderrama



More information about the Grok-dev mailing list