Welcome Guest, Not a member yet? Register   Sign In
Error Handling Discussion
#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.


Messages In This Thread
Error Handling Discussion - by El Forum - 04-17-2008, 11:11 AM
Error Handling Discussion - by El Forum - 04-17-2008, 02:46 PM
Error Handling Discussion - by El Forum - 04-21-2008, 12:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB