Error Handling For All Actions In A Certain Controller (And Only In It)

Hi,

I have this module which works mainly as a JSON provider to client side.

I wish all exceptions in the controller actions will be caught internally, handled (logged) and a nicely formatted JSON error message will be sent to client side.

What would you recommend in order to achieve this?

Thanks,

Boaz

In this controller (in the init function for example) you can change the error action of Yii::app()->errorHandler to an action of the controller itself that sends a json formatted exception.

I see what you mean.

But I’m thinking - changing Yii::app()->errorHandler will affect anything that runs in this request right? I mean, if I have some method that runs at the end of the request, such as CApplication->onEndRequest(), it will run always with the modified error handler right?

I can use this solution but if I’m right above (with regard to other methods running on the same request) then this is not the cleanest solution. Maybe it doesn’t get any cleaner but that’s exactly the question…

Thanks for the swift reply!

Also, isn’t there some built in attribute or otherwise method to achieve exactly what I’m looking for?

Maybe an extension? (didn’t find any).

In a nutshell, I’m looking for “per controller error handler”, or per controller actions.

Hmm… Gotcha! I’ll utilize filters for this. I never use filter with ‘pre’ and ‘post’ methods - time to dive into it!

I think that this solution is clean enough.

The error handler is supposed to handle error, and the action error is a writeable parameter, so why not to use?

I think that mere setting of Yii::app()->errorHandler->errorAction to a route in the specific controller is even sleeker since its less intrusive - no need to provide a complete error handler replacement component but rather only an action method.

I already implemented this as an extension which I’m uploading as I write this, since I wasn’t sure if Yii supports returning back to request processing after the method pointed by errorAction is being run.