• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
show_error bug?

#1
[eluser]JayKae[/eluser]
So I have been trying to extend the default CI_Exceptions class. But what is driving me crazy is that it isn't working properly.

I'm using CI 2.0.2 excerpt below is from system/core/Exceptions.php
Code:
/**
     * 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', $status_code = 500)
    {
// My debugging logic starts here
        print('Heading: '.$heading);
        echo '<br>';
        print('Message: '.$message);
        echo '<br>';
        print($template);
        echo '<br>';
        print($status_code);
        echo '<br>';
// Debug logic ends here

        set_status_header($status_code);

        $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

        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;
    }

Adding some debug logic was my only change to the function. I thought it might be something I was doing wrong, so I renamed MY_Exceptions.php to something else so it is not coming into play.

Simply calling
Code:
show_error('$heading', '$message', '$template', 404);
results in the following output

Quote: Heading: $template
Message: $heading
error_general
$message
Heading: An Error Was Encountered
Message: Status codes must be numeric
error_general
500

An Error Was Encountered
Status codes must be numeric

I found the reason that it isn't working. In system/core/Common.php the following method is declared.
Code:
/**
* Error Handler
*
* This function lets us invoke the exception class and
* display errors using the standard error template located
* in application/errors/errors.php
* This function will send the error page directly to the
* browser and exit.
*
* @access    public
* @return    void
*/
    function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
    {
        $_error =& load_class('Exceptions', 'core');
        echo $_error->show_error($heading, $message, 'error_general', $status_code);
        exit;
    }

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

The function signatures aren't the same and that is what is causing the issue. I am assuming this is an oversight and I should file a bug report but I want to make sure that I haven't overlooked anything, I'm only 4 days into learning CI.

#2
[eluser]InsiteFX[/eluser]
An Error Was Encountered
Status codes must be numeric

Try this:
Code:
function show_error($heading, $message, $template = 'error_general', (int)$status_code = 500)

set_status_header((int)$status_code);

InsiteFX

#3
[eluser]JayKae[/eluser]
The actual issue is that show_error in CI_Exception signature is:
Code:
show_error($heading, $message, $template = 'error_general', $status_code = 500)
and in Common the signature is:
Code:
show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')

The signatures are off but show_error in common calls the show_error in exception, so I have no way of access the actual CI_Exception arguments

#4
[eluser]InsiteFX[/eluser]
Oh, I see what you mean now, you should file a BUG report on the Reactor Bitbucket.

InsiteFX

#5
[eluser]Unknown[/eluser]
I found issues trying to get this to work as well. I used this example to start: http://www.outofrepose.com/2010/02/17/ex...ons-class/

Two things:
1. JayKae is right, the two signatures are different which adds to confusion for someone not well versed in CI but its manageable.
2. MY_Exceptions.php should be in core and not libraries. (or whatever you prefix your custom class files with).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.