views:

93

answers:

1

Hello all

I am creating a webapp using codeigniter. I want to implement a error handling function. Say for example if I call a method of a model, and if an error occurs in that method, the error handler comes into action to return some pre-formatted string.

I was thinking of creating something like MY_Model, which every model class extends. Then, I can add the error handler in MY_Model class. But whether this can be done is beyond me right now. (yes I am a newbie at this)

Any enlightening ideas will help.

Regards

+1  A: 

What I tend to do is return an array instead of a boolean. This array contains 2 keys, 'return' and 'error'.

In case of an error this array will look like the following:

array('return' => FALSE, 'error' => 'Some error')

In case of a successful execution this array will look like the following:

array('return' => TRUE)

The controller then verifies these results and if there's an error it will display the one set in the 'error' key.

Yorick Peterse
@Yorick - That's how you have implemented the error handling. I want to know where to implement it?
ShiVik
Those arrays are returned by the model to the controller which then validates it.
Yorick Peterse
Does that mean putting a try catch inside every method of model, so that when an exception is caught, the array is returned??
ShiVik
Here's an example taken from an actual project: http://pastie.org/1055428
Yorick Peterse
@Yorick - Thanks for that. This helped me out.
ShiVik
Your welcome! :)
Yorick Peterse