Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Catching Database Connection Exception
  • I've been doing various scenario testing to make sure our application catches exceptions well, but I am unable to properly catch an exception when the database connection fails. I am using this as a test: try
    {
    \Database_Connection::instance()->connect();
    }
    catch (\Database_Exception $e)
    {
    die('Database Connection Error: '.$e->getMessage());
    }
    This is the result I get, showing there are some errors that I can't catch: Warning! ErrorException [ Warning ]: mysql_connect(): No such file or directory COREPATH/classes/database/mysql/connection.php @ line 96: 95: // Create a connection and force it to be a new link
    96: $this->_connection = mysql_connect($hostname, $username, $password, true);
    97: }
    Warning! ErrorException [ Warning ]: mysql_select_db() expects parameter 2 to be resource, boolean given COREPATH/classes/database/mysql/connection.php @ line 127: 126: {
    127: if ( ! mysql_select_db($database, $this->_connection))
    128: {
    Warning! ErrorException [ Warning ]: mysql_error() expects parameter 1 to be resource, boolean given COREPATH/classes/database/mysql/connection.php @ line 130: 129: // Unable to select database
    130: throw new \Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
    131: }
    Warning! ErrorException [ Warning ]: mysql_errno() expects parameter 1 to be resource, boolean given COREPATH/classes/database/mysql/connection.php @ line 130: 129: // Unable to select database
    130: throw new \Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
    131: }
    Database Connection :
  • You can catch Database_Exception, as that is the only real exceptions in there. The others are PHP errors or warnings, which can't be caught. Day in the life of a PHP programmer, I'm afraid. These errors are all programming errors, and should be fixed long before the application reaches production state. We have chosen to not overload the core code with parameter validation, as it would only serve to protect from programmers error, but would make the framework slower at every request.
  • I appreciate your response, as usual :) The thing is these are not application / programming errors, so there is nothing that I can fix. The scenario is this: The app works, without error, but if the mysql db is offline, the errors that I posted appear. There is no way for me to catch those errors, that I can find. Does that make sense? I want to catch those errors and display a nice message to the user, and not reveal the innards of the app. You can recreate this by putting that try catch block in your code, seeing that there are no errors, and then turning the db off. How do I catch that scenario? Or am I totally missing something?
  • I understand what you want. But unfortunately PHP doesn't generate exceptions for these errors, it just outputs a PHP warning (or error), which can not be caught.
  • I understand your original suggestion about dealing with those errors in the PHP code. The production version of the app wont display errors or notices, and the exception will be caught. But in this instance, the exception is being caught, but the exception message is not being passed. You will notice the final output in my post above is the Database Exception: but the message is not being passed. Can you help me shed some light on that? I am finding the same issue trying to catch a twig syntax exception message.
  • As said before, those are not exceptions, they are PHP warnings, errors, notices, and can not be caught using try/catch. It could be a possibility to have a PHP error handler catch them and convert them to Exceptions, I'll have to see if that's feasible and if we want to do that.
  • I believe the last one is an exception and it is the one I cant get the message from. It does say
    throw new \Database_Exception ErrorException [ Warning ]: mysql_errno() expects parameter 1 to be resource, boolean given COREPATH/classes/database/mysql/connection.php @ line 130:
    129: // Unable to select database
    130: throw new \Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
    131: } My output: Database Connection : My Code: try
    {
    \Database_Connection::instance()->connect();
    }
    catch (\Database_Exception $e)
    {
    die('Database Connection Error: '.$e->getMessage());
    }
    Again. I appreciate your help tremendously.
  • It doesn't get to the throw() because mysql_errno() causes a PHP error. I've just committed an update of the Error class to 1.3/develop which allows you to control which old-style PHP errors will throw an exception through the "errors.continue_on" configuration key (in app/config/config.php). By default it is set to
    'continue_on'  => array(E_NOTICE, E_WARNING, E_DEPRECATED, E_STRICT),
    
    but if you remove E_WARNING from here, the new class will now throw a PhpErrorException on E_WARNING errors.
  • I didnt realize the PHP errors were causing the exception not to be thrown, but that makes complete sense now that you've pointed it out. Thank you for the dev update. I will start playing with that branch. Thank you again.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion