tags:

views:

150

answers:

1

Suppose I have a controller method like so:

@expose()
def search(self, title):
    return dict()

Going to http://site/search/ will cause an exception to be thrown: TypeError: search() takes exactly 2 arguments (1 given).

The error is logical, but I'd rather handle it more gracefully. Is using *args or **kwargs the only way to avoid an error that I don't even seem to be able to catch?

EDIT: I guess I could always use title=None, but too much of that could get ugly...

Anyway, is there a way to catch the exception and/or handle argument mismatches more gracefully?

Thanks

+1  A: 

Hello there.

The exception thrown at you for specifying an "incompatible" controller method signature only happens in debug / development mode. You dont need to handle it more gracefully in a production environment, because once you disable development mode, controller methods send an HTTP 500 Error when they lack essential parameters.

You might want to consider the respective settings in your development.ini:

# WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT*
# Debug mode will enable the interactive debugging tool, allowing ANYONE to
# execute malicious code after an exception is raised.
set debug = false

I hope this was your question.

In the case that you still want the controller do its work, even though its lacks important parameters, you must define default values, else the controller cannot do its work properly anyway. The question you better ask yourself is: Do you simply want a nicer error message, or do you want the controller to be able to do its task. In the latter case, specifying default parameters is best practise, *args and **kwargs for each method just so the customer doesnt get an error is a very ugly hack in my option.

If you want to change the display of these errors refer to /controllers/error.py

Hope this helped,

Tom

Tom
My question was mostly involved with how best to handle errors like this. I think you answered it best when you mentioned /controllers/error.py. I'll take a look at that. Thanks.