Search code examples
phpdatabaseexceptionkohanakohana-3.2

How to set an own Exception on wrong database connection?


When I'm trying to connect to wrong database I'm getting standart kohana exception message like this:

Database_Exception [ 1049 ]: Unknown database 'mywrongdatabase'
MODPATH\database\classes\kohana\database\mysql.php [ 108 ]
    protected function _select_db($database)
    {
        if ( ! mysql_select_db($database, $this->_connection))
        {
            // Unable to select database
[line 108]          throw new Database_Exception(':error',
                array(':error' => mysql_error($this->_connection)),
                mysql_errno($this->_connection));
        }

        Database_MySQL::$_current_databases[$this->_connection_id] = $database;

This came out from file

MODPATH\database\classes\kohana\database\mysql.php [ 108 ]

How to set (and where) own message instead of standart kohana's exception? Also I don't want to modify any kohana standart modules (like database) or system files.


Solution

  • You can try with try/catch, but I left both error_log and throw new Exception because I don't know which one you need.

    try{
        //code with connection
    } catch (Exception $e){  
        error_log("This is my own message");
        throw new Exception( 'Something really gone wrong', 0, $e);
    }