2

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.

ljubiccica
  • 480
  • 3
  • 17
Smash
  • 513
  • 5
  • 23

1 Answers1

1

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);
} 
ljubiccica
  • 480
  • 3
  • 17
  • where I cand do that? Or it is possible only inside a module file? – Smash May 14 '13 at 19:47
  • 1
    I found this question and answer here on SO - you can try it out: http://stackoverflow.com/questions/6831402/custom-exception-handling-in-kohana3 – ljubiccica May 14 '13 at 20:02
  • Okey, I found out that I can realy override kohana exception class as he did, but not the custom error, because in my example I have `Database_Exception` and thats can be a many things, like wrong db name or the user/password data. Instead of what he has in his example: strikt `HTTP_Exception_404` error handle. – Smash May 14 '13 at 21:24
  • Maybe to check this in the `case` block... hmm – Smash May 14 '13 at 21:26
  • There has to be "Database_Exception" somewhere in your code. Try to search for it and make the same thing he did for HTTP_Exception. – ljubiccica May 14 '13 at 21:29
  • Thats what I'm saying, he didn't did an `HTTP_Exception`, he did `HTTP_Exception_404` and thats a different staffs, isn't? – Smash May 14 '13 at 21:37
  • Would be cool if it will something like this: `case 'Database_Exception_1049': echo 'Wrong Database'; return TRUE; break;` – Smash May 14 '13 at 21:53