Welcome Guest, Not a member yet? Register   Sign In
Error Handling Discussion
#1

[eluser]Jim Higgins[/eluser]
I'm really trying to wrap my head around the process of error handling and proper user feedback for errors. Can anyone point me towards some good resources, examples, posts, or help explain the "rules" or best practice?

I'm curious as to when and where I should use try/catch statements? And the proper syntax for using them?

Additionally, I'm curious as to where/how to show error messages for the user when combined with the above try/catch statements?

Just to preclude the standard response "See the Error Handling" class... Thank you for that response in advance. I'm looking to expand the discussion and see some more in-depth usage and handling by others.

For example, when I say "where" above, I'm referring to things like: "I like to put them in my model whenever I do this... and this is how I do it. Then I throw the user a message like... and this is how I do it... etc, etc, etc"

Thanks
#2

[eluser]Jamie Rumbelow[/eluser]
There are some good built in php functions for this kind of thing. They are a bit hard to get used too, but you can get the exact line where the parse error occured - it's very powerful.

See this: http://uk2.php.net/manual/en/function.se...andler.php
#3

[eluser]mademan[/eluser]
The way I did error handling in a production CI environment was to override CI's error handler by using a post_controller_constructor hook. The main job of this custom error handler is to display a simple error message to the user and send me an email with the problem details.

Here's a simplified version of the custom error handler:

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

function run()
{  
    error_reporting(0);          

    set_error_handler('my_error_handler');
}

function my_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
    switch ($errno)
    {
        case E_ERROR:
        case E_USER_ERROR:  
            my_show_error('There was a problem (ERROR) running the script.<br /><br />
                           The system support team has been notified via email');
            send_mail("ERROR\nMessage: $errstr\nFile: $errfile\nLine:$errline");
            die();  
            break;
        case E_WARNING:              
        case E_USER_WARNING:
            my_show_error('There was a problem (WARNING) running the script.<br /><br />
                           The system support team has been notified via email');
            send_mail("WARNING\nMessage: $errstr\nFile: $errfile\nLine:$errline");
            die();
            break;                      
    }
}

function my_show_error($message)
{
?&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;CI Application Problem&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url()?&gt;css/style.css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <div id="content">
            &lt;?php echo $message; ?&gt;
        </div>        
    &lt;/body&gt;
    &lt;/html&gt;
    &lt;?php
}

function send_mail($message)
{    
    $CI =& get_instance();
    $CI->email->from($CI->config->item('s_email'), 'CI Engine');
    $CI->email->to($CI->config->item('s_email'));
    $CI->email->subject('CI Application Problem');
    $CI->email->message($message);
    $CI->email->send();
}
?&gt;

In my models I use triggers when an error occurs that my custom error handler will catch and handle:

Code:
&lt;?php
class User_model extends Model
{
    public function User_model()
    {
        // call the Model constructor
        parent::Model();
    }

    public function get_users()
    {      
        $sql = '
        SELECT
            *
        FROM
            users';

        $result = $this->db->query($sql);
        
        if (!$result)
        {
            trigger_error ('Problem with query: ' . mysql_error ( ) . ' ' . $sql, E_USER_ERROR);          
        }
        else
        {
            return $result;
        }
    }
}

I used the try/catch error handler method but it seemed to over complicate and make the code base larger with no real advantage to using this type of error handling method for my purpose. I didn't need unnecessary code getting in the way. I needed something simple and unobstructive.

During development I revert back to using CI default error and debugging handlers.

I am curious to see how others are implementing there error handling as well.




Theme © iAndrew 2016 - Forum software by © MyBB