Welcome Guest, Not a member yet? Register   Sign In
custom error tracking and extending exceptions/routes
#1

[eluser]AtlantixMedia[/eluser]
I would like to hear from someone who has successfully implemented a custom error tracking system with db logging and custom 404 pages. what I'm trying to accomplish is:

1) Saving errors to a db table. I'm partially able to accomplish this by extending the exceptions class. please see below.
2) show a custom 404 page. I'm partially able to accomplish this by extending the router class. please see below. I say partially because this system seems to work when I input a totally fake URL. i.e.: www.domain.com/nonexistent. however, if I do something like this: www.domain.com/media/video/shownonexistent where www.domain.com/media/video/show points to a real controller functions, CI shows the default 404 page.

I would like to start a discussion on how to effectively build a custom error tracking and 404 pages as I believe many others will benefit from it.

with regard to #1, is extending the exceptions class the proper way to approach this?

thanks in advance

MY_Exceptions

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

class MY_Exceptions extends CI_Exceptions{
    
    function MY_Exceptions(){
        parent::CI_Exceptions();
        
    }

          
    // --------------------------------------------------------------------

    /**
     * General Error Page
     *
     * This function takes an error message as input
     * (either as a string or an array) and displays
     * it using the specified template.
     *
     * @access    private
     * @param    string    the heading
     * @param    string    the message
     * @param    string    the template name
     * @return    string
     */
    function show_error($heading, $message, $template = 'error_general')
    {
        $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

        $CI = & get_instance();
        if ($CI->config->item('EnableErrorLogging') == 1)
        {
            switch($template)
            {
                case 'error_general':    $type = 'GEN';    break;
                case 'error_404':        $type = '404';    break;
                case 'error_db':        $type = 'DB';    break;
                case 'error_php':        $type = 'PHP';    break;
                default:                $type = 'GEN';
            
            }
            
            $CI->load->model('Admin_Model');
            $CI->Admin_Model->LogError_Q($type, '', $message, '', '');
        }
        

        if ($CI->config->item('ShowErrors') == 1)
        {
        
            if (ob_get_level() > $this->ob_level + 1)
            {
                ob_end_flush();    
            }
            ob_start();
            include(APPPATH.'errors/'.$template.EXT);
            $buffer = ob_get_contents();
            ob_end_clean();
            return $buffer;
        }    
    }

    // --------------------------------------------------------------------

    /**
     * Native PHP error handler
     *
     * @access    private
     * @param    string    the error severity
     * @param    string    the error string
     * @param    string    the error filepath
     * @param    string    the error line number
     * @return    string
     */
    function show_php_error($severity, $message, $filepath, $line)
    {    
        $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
    
        $filepath = str_replace("\\", "/", $filepath);
        
        // For safety reasons we do not show the full file path
        if (FALSE !== strpos($filepath, '/'))
        {
            $x = explode('/', $filepath);
            $filepath = $x[count($x)-2].'/'.end($x);
        }
        
        $CI = & get_instance();
        if ($CI->config->item('EnableErrorLogging') == 1)
        {
            $CI->load->model('Admin_Model');
            $CI->Admin_Model->LogError_Q('PHP', $severity, $message, $filepath, $line);
        }        
        
        if ($CI->config->item('ShowErrors') == 1)
        {
        
            if (ob_get_level() > $this->ob_level + 1)
            {
                ob_end_flush();    
            }
            ob_start();
            include(APPPATH.'errors/error_php'.EXT);
            $buffer = ob_get_contents();
            ob_end_clean();
            echo $buffer;
        }
        
    }
    
}

?&gt;
#2

[eluser]AtlantixMedia[/eluser]
MY_Router

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

class MY_Router extends CI_Router{
    
    function MY_Router(){
        parent::CI_Router();
    }
    
    function _validate_segments($segments)

    {

        // Does the requested controller exist in the root folder?

        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))

        {

            return $segments;

        }



        // Is the controller in a sub-folder?

        if (is_dir(APPPATH.'controllers/'.$segments[0]))

        {        

            // Set the directory and remove it from the segment array

            $this->set_directory($segments[0]);

            $segments = array_slice($segments, 1);

            

            if (count($segments) > 0)

            {

                // Does the requested controller exist in the sub-folder?

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))

                {
                    # HENRIK PEJER MOD: commented out the line below, added the line below that

                    #show_404();
                    return $this->custom_404();

                }

            }

            else

            {

                $this->set_class($this->default_controller);

                $this->set_method('index');

            

                // Does the default controller exist in the sub-folder?

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))

                {

                    $this->directory = '';

                    return array();

                }

            

            }              

            return $segments;

        }

    

        // Can't find the requested controller...
        # HENRIK PEJER MOD: commented out the line below, added the line below that
        #show_404();
        return $this->custom_404();

    }
    
    function custom_404(){
        # return an array with the name of the controller we want as the 404-handler...
        return array('custom404');
    }
}

?&gt;
#3

[eluser]AtlantixMedia[/eluser]
nobody wants to take a stab at this?
#4

[eluser]AtlantixMedia[/eluser]
can't believe nobody knows about this
#5

[eluser]AtlantixMedia[/eluser]
bumping the thread




Theme © iAndrew 2016 - Forum software by © MyBB