Handling Exceptions in CodeIgniter

I have been studying PHP

with CodeIgniter

Framework lately . I'm working on exception handling, but one thing surprised me that exception handling doesn't work with database error when there is a parameter db_debug = TRUE

in database.php

.

If db_debug = FALSE

, I can throw and catch the error, but when it does TRUE

, it immediately displays the database error page.

Here is my code:

In Model

:

    $this->db->insert('tbl_name',$data);
    if (strpos($this->db->_error_message(),'constraint_name') !== FALSE)
    {
        throw new UniqueConstraintException($this->db->_error_message()); // UniqueConstraintException is customized excpetion
    }

      

In Controller

:

        try
        {
             //calling the method from model having above code
        }
        catch(UniqueConstraintException $ex)
        {
              echo "There is already item with same item name";
        }
        catch(Exception $ex)
        {
             echo $ex->getMessage();
        }

      

Is it possible to implement database error exception handling even if there is a parameter db_debug = TRUE

?

+3


source to share


1 answer


The CodeIgniter guys don't know anything about oop. When you go through the code, you'll find things like this a million times in the DB driver (don't know anything about DRY):

if (<some condition>) {
    if ($this->db_debug) {
        log_message('error', 'Invalid query: '.$sql);
        return $this->display_error('db_invalid_query');
    }
    return FALSE;
}

      



No, you cannot disable this "feature". However, you could extend the CI_DB_driver class to get rid of this problem, but due to the lack of DRY code, you will have to override almost all of this class ...

+9


source







All Articles