Christoph Zwerschke wrote:
>> So the tg_errors argument has been passed correctly and the dummy
>> variable still contains the Root object. This made me believe I need
>> the dummy variable.
>>
>> I don't understand fully what's going on especially because Chris
>> suggested that the dummy variable is not needed. Any ideas what is
>> really happening behind the scenes?
>>
>
> I've looked into errorhandling again and you're right, the first
> argument of an error handler is always the controller. I believed the
> error handler is a bound method, but at the time when you're assigning
> the error handler, it's not a bound method yet. So the signature has to
> be compatible to the unbound method. That's why you need the dummy
> parameter, just name it "controller" to make this more clear. Sorry to
> be misleading.
>
Yes, the first parameter in an error_handler or exception_handler must
be a function - or an unbound method.
Partial functions from functools, and callable objects are not supported
and will be rejected by inspect.getargspec():
def getargspec(func):
if ismethod(func):
func = func.im_func
if not isfunction(func):
raise TypeError('arg is not a Python function')
args, varargs, varkw = getargs(func.func_code)
return args, varargs, varkw, func.func_defaults
I think it would be a common use case to provide an error-handler
Factory with some parameters, but I found out inspect.py would not let me.
See at the bottom of the message for a monkey patch to use any callable
as an error or exception handler.
In this example I initialize the handler with a "next-page" value to be
redirected at after handling the exception, and a message to be displayed:
> @expose()
> @exception_handler(ExceptionHandlerFactory('./index', 'Could not
> do this and that'), 'isinstance(tg_exceptions, MyRootException)')
> def do_this_and_that(self, ...):
> ...
The exception handler factory returns an handler that
- displays "Could not do this and that - " + tg_exceptions.message
- redirects to './index'
- converts form parameters from POST to GET (they will be picked up as
default values in the form, in case the data is formally correct but the
operation has triggered an exception)
> class ExceptionsHandlerFactory(object):
> def __init__(self, newpage, msg=None):
> self.newpage = newpage
> self.msg = msg
>
> def __call__(self, *args, **kw):
> """
> Generic exception handler to display a warning flash and
> redirect using the provided newpage.
> Any POST arguments are converted to GET.
> """
> tg_exceptions = kw['tg_exceptions']
> if self.msg:
> wrn = self.msg + ' - '
> if tg_exceptions.message:
> wrn = wrn + tg_exceptions.message
> wrn = wrn.lstrip().strip(' -')
> flash.warning(wrn)
> params = NestedVariables.from_python(cherrypy.request.params)
> params['_override'] = '1'
> params.pop('submit', None)
> raise tg.redirect(self.newpage, params)
Unfortunately, to make this work, I had to sell my soul to the Ruby
daemon and monkeypatch a standard library module.
> import inspect
> from inspect import isfunction, ismethod, getargs
>
> def getargspec(func):
> if getattr(func, '__call__') and not isfunction(func) and not
> ismethod(func):
> func = func.__call__
> if ismethod(func):
> func = func.im_func
> if not isfunction(func):
> raise TypeError('arg is not a Python function')
> args, varargs, varkw = getargs(func.func_code)
> return args, varargs, varkw, func.func_defaults
>
> inspect.getargspec = getargspec
--
This e-mail (and any attachment(s)) is strictly confidential and for use only
by intended recipient(s). Any use, distribution, reproduction or disclosure by
any other person is strictly prohibited. The content of this e-mail does not
constitute a commitment by the Company except where provided for in a written
agreement between this e-mail addressee and the Company.
If you are not an intended recipient(s), please notify the sender promptly and
destroy this message and its attachments without reading or saving it in any
manner.
Any non authorized use of the content of this message constitutes a violation
of the obligation to abstain from learning of the correspondence among other
subjects, except for more serious offence, and exposes the person responsible
to the relevant consequences.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---