Saturday, March 27, 2010

Re: try/catch action

Well then I'm back to the solution I suggested in my initial post: I
think it would be as simple as throwing the try catch around the
Action invocation in the dispatcher.

I will play around with it. I really don't want to touch cake
internals, but it makes more sense than the three alternatives I see:
(1) Possibly letting some exceptions slip by during beta testing. (2)
Internally placing the same try/catch wrapper in every single Action
(3) Writing try/catch statements for every single possible exception
before knowing that they will ever be thrown.

My plan is to use this try/catch Action wrapper with the intention of
refining exception handling for frequent exceptions.

I'm sure there will be an elegant Cake solution in 2.0 or perhaps
sooner. In the meantime, I'm going to work with this catch-all and
keep my eye out for a way to solve this better.

On Mar 24, 4:44 pm, Miles J <mileswjohn...@gmail.com> wrote:
> I do not believe there is a way that you can apply this functionality
> to multiple actions, it would have to be written out in each one. The
> most I can see is doing this:
>
> public function action() {
>         try {
>                 // Do something here
>         } catch(Exception $e) {
>                 $this->_throwError($e->getMessage());
>         }
>
> }
>
> protected function _throwError($msg) {
>         echo json_encode(array(
>                 'error' => true,
>                 'message' => $msg
>         ));
>
> }
>
> On Mar 24, 1:11 pm, Josh <joshs.silver...@gmail.com> wrote:
>
>
>
> > That's what I have more or less.
>
> > But, what I want to do is apply that in bulk to all actions as a
> > backstop to any uncaught Exceptions.
>
> > I gave the above example to demonstrate when this might be useful. To
> > clarify, I have maybe 20 Ajax actions, all of which are called with
> > the Prototype Request object. I want to make sure that the onFailure
> > function on the Prototype Request object is always triggered if there
> > is an Exception thrown in the back-end (unless that Exception is
> > caught earlier).  It would be redundant to wrap the logic on the
> > interior of each action with a try/catch statement and generic
> > handling.
>
> > To avoid this redundancy, I would like to wrap the action call in the
> > dispatcher with generic Exception handling.
>
> > On Mar 24, 1:49 pm, Miles J <mileswjohn...@gmail.com> wrote:
>
> > > 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 <joshs.silver...@gmail.com> 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 <joshs.silver...@gmail.com> 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 <j.andersen...@gmail.com> 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 <joshs.silver...@gmail.com> 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com 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.

No comments: