tags:

views:

262

answers:

1

I would like to somehow apply a try catch statement to all Actions as a backstop for any uncaught exceptions.

I think this would be particularly helpful for Ajax Actions, because the catch statement could send back a default 4xx status code. Prototype's onFailure() function could then do the client-side error handling.

How can I do this without wrapping the Action call with a try/catch in the cake dispatcher like this:

try {
    output = $controller->dispatchMethod($params['action'], $params['pass']);
}
catch {...}

Does anybody have a suggestion or another workable strategy for gaining this functionality without touching the dispatcher?

How do people feel about putting exception handling in the Displatcher? I imagine when cake drops php 4 support, there will be a built-in mechanism for this.

[Edit] I've looked into the cake Error Handling. Without try/catch it seems like there is a big loss in functionality. And, I am hesitant to mix cakeErrors and other Exceptions.

+1  A: 

I'm not too sure why you would want to do this. You can check the params to see if it's an ajax call in the controller using,

if($this->params['requested'])

I don't know what kinds of exceptions your application might create, but if it's a missing action, view or similar, Cake will output an error page, which you can customise in the app/views/errors folder.

The book has some information on how to handle errors using CakePHP's built in error handler here, http://book.cakephp.org/view/154/Error-Handling

You could also have a google around for articles and tutorials on creating your own custom error handler, or extending the built in one, so that it wraps all dispatch calls in a try{}catch{} from the core of the dispatch cycle. 'cakephp custom error handler' google link ( http://u.nu/8bgv7 ) although I think those articles might be a bit old, so check the date against what version of Cake you're using.

DavidYell
These aren't generic errors like 'missing action'. They are often generated by contacting other web-services or working with other applications running on the server (ie image generator) or my cache-engine. They are generally handled by exception handling in the Action. I was just looking for a backstop that can provide some universal handling for exceptions that aren't caught. This would be useful in the example I gave. Thanks for the link. I'll look into doing this the PHP 4/Cake way: the ErrorHandler class. Thanks.
joshs
Ah I see, for something like that I could catch and process the error codes in the actions which call the external services and then switch on the response code, and throw a cake error.
DavidYell
The ultimate goal might be to catch all Exceptions in the Action, but it would be nice to have a backstop... particularly during beta-testing. Also, the point of Exceptions is to move unexpected case handling outside of the normal case logic. I don't want my actions to become elaborate Exception handlers... particularly during development when it's unclear which Exceptions will actually be thrown frequently.
joshs