I believe you can just do something like this, especially if its an
AJAX call and you want to simply return a json response.

public function action() {
        try {
                // Do something here
        } catch(Exception $e) {
                echo json_encode(array(
                        'error' => true,
                        'message' => $e->getMessage()
                ));
        }
}

On Mar 24, 8:47 am, Josh <[email protected]> wrote:
> I've looked into the Cake Error Handling. Without try/catch it seems
> like there is a big loss in functionality. Exceptions allow you to
> handle unexpected behaviors outside of a normal case flow. CakeErrors
> seem intended for stopping the app and giving the user feedback.
>
> And, I am hesitant to mix CakeErrors and Exceptions.
>
> Does anybody have any comments on using try/catch in the dispatcher as
> a backstop for uncaught exceptions?
>
> On Mar 24, 11:28 am, Josh <[email protected]> wrote:
>
> > The exceptions wouldn't be generated by a missing method. 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. But, does anybody have a good solution using
> > Exceptions and try/catch?
>
> > Thanks.
>
> > On Mar 24, 2:46 am, John Andersen <[email protected]> wrote:
>
> > > As far as I remember, you don't need to do try catch, just compare the
> > > current action against the actions defined in the controller. For
> > > example you can do the following (quick and dirty solution) in the
> > > AppController beforeFilter method:
>
> > > [code]
> > > if (!in_array($this->params['action'], get_class_methods($this))) {
> > >    do your error processing}
>
> > > [/code]
>
> > > or even better, you can try to make your own cakeError processing.
> > > See:http://book.cakephp.org/view/154/Error-Handling
>
> > > Enjoy,
> > >    John
>
> > > On Mar 24, 6:43 am, Josh <[email protected]> wrote:
>
> > > > Hello
>
> > > > 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 ($controller->dispatchMethod($params['action'],
> > > > $params['pass']);)
>
> > > > Does anybody have a suggestion or another workable strategy for
> > > > gaining this functionality?
>
> > > > Josh

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" 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/cake-php?hl=en

To unsubscribe from this group, send email to 
cake-php+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to