Welcome Guest, Not a member yet? Register   Sign In
Graceful error handling
#1

[eluser]tim1965[/eluser]
Hi

I have nearly completed my first website using Codigniter, Hurrah. However this is my first website period and my exceptions are not so graceful!!
I have started to look at putting in exception handling for the code. I think i need to do this at two levels : within the controller itself and within each function. What i am struggling with is do i need to test for the existence of everything and throw errros ?
So what would be useful to get me going is to see other peoples error code in an insert function for example i.e. what you put in the function and how you structure it and what you have in the controller. This would show me what sort of level of handling i need to go down to i.e. test for vars or an array, etc.
Also any suggestions or approaches people use.
Many thanks in advance.
#2

[eluser]Damien K.[/eluser]
Some people live by throwing exceptions everywhere. My preference is to not handle any error exception in the controller. Furthermore, exceptions are only thrown on fatal errors (I've seen it use as a messaging system before). However, in PHP I tend to have a preference not to use exceptions. When I do use exceptions, it will look something like this, in pseudo code:

Code:
class A_model
{
    ...


    function insert($data_value_object)
    {
        try
        {
            // try to insert dvo into database
            // but say, for example, an error occurs
            // (eg, cannot connect to db, primary key already exists, etc.)
        }
        catch ($exception)
        {
            // log error
            // throw exception
        }
    }

    ...
}

class A_controller extends Controller
{
    ...

    function add_item()
    {
        $dvo = get_post_data();  // fill with post data
        $this->A_model->insert($dvo);
    }

    ...
}

As you can see, the controller doesn't try to catch any exception. I would usually catch the exception at the last tier and if caught will display an generic application error page. You can optionally choose to set a handler with set_exception_handler() to handle your uncaught exceptions.

For non-fatal errors, the model will return something else so the controller can gracefully inform the user (eg, "Cannot sign up with specified email address because it already exists.").




Theme © iAndrew 2016 - Forum software by © MyBB