Welcome Guest, Not a member yet? Register   Sign In
Extend Exceptions Class for DB Errors
#1

[eluser]Pascal Kriete[/eluser]
I've already extended the Exceptions class for php, general, and 404 errors. So I figured I could just add db errors as well.

I was wrong.
As it is right now the code looks like this:
Code:
function display_error($error = '', $swap = '', $native = FALSE)
{
//    $LANG = new CI_Lang();
    $LANG = new CI_Language();
    $LANG->load('db');

    $heading = 'Database Error';
    
    if ($native == TRUE)
    {
        $message = $error;
    }
    else
    {
        $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
    }

    if ( ! class_exists('CI_Exceptions'))
    {
//        include(BASEPATH.'core/Exceptions'.EXT);
        include(BASEPATH.'libraries/Exceptions'.EXT);
    }
    
    $error = new CI_Exceptions();
    echo $error->show_error('An Error Was Encountered', $message, 'error_db');
    exit;
}

What the?

The biggest issue here is that it creates a new Exceptions object. Why?
What's wrong with just calling the show_error method declared in common.php?

Also, why does it set $header if it's not used?

Should be something more like this:
Code:
function display_error($error = '', $swap = '', $native = FALSE)
{
    $LANG =& load_class('Language');
    $LANG->load('db');

    $heading = 'Database Error';
    
    if ($native == TRUE)
    {
        $message = $error;
    }
    else
    {
        $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
    }

    show_error($heading, $message, 'error_db');
}

EDIT: Ok, doesn't quite work like that because show_error only takes on argument. The point remains however.
#2

[eluser]Pascal Kriete[/eluser]
Here we go, this version works properly.
Code:
function display_error($error = '', $swap = '', $native = FALSE)
{
    $LANG =& load_class('Language');
    $LANG->load('db');

    $heading = 'Database Error';

    if ($native == TRUE)
    {
        $message = $error;
    }
    else
    {
        $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
    }

    $EXC =& load_class('Exceptions');
    echo $EXC->show_error($heading, $message, 'error_db');
    exit;
}




Theme © iAndrew 2016 - Forum software by © MyBB