views:

421

answers:

3

I'm using Kohana 2. I would like to catch a database exception to prevent an error page when no connection to the server can be established.

The error displayed is

system/libraries/drivers/Database/Mysql.php [61]:

mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at
'reading initial communication packet', system error: 110

The database server is not reachable at all at this point.

I'm doing this from a model. I tried both

public function __construct()
{
    // load database library into $this->db
    try
    {
        parent::__construct();
    }
    catch (Exception $e)
    {
        die('Database error occured');
    }
}

as well as

try
{
    $hoststatus = $this->db->query('SELECT x FROM y WHERE z;');
}
catch (Exception $e)
{
    die('Database error occured');
}

...but none of them seemed to work. It seems as if no exception gets passed on from the main model. Is there another way to catch the database error and use my own error handling?

A: 

I don't know Kohana, but a try .. catch block will not catch normal errors, only Exceptions. Are you sure Kohana throws Exceptions where you expect to receive Exceptions?

Edit based on you comments:
Well, first of all, in a production environment (meaning your live application) you should always disable the displaying of PHP errors to the screen. Displaying of these errors to the screen should only be done in a development environment to inform you, the developer. Visitors of your live application however have no business in knowing/reading PHP errors, as it might disclose sensitive information about your environment. You should however log the errors to a log file.

Furthermore, I just took a quick peek at Kohana, and indeed see that here and there Exceptions are thrown, but it doesn't seem to do this in a consistent manner.

If you want php errors to be treated as Exceptions have a look example #1 in this documentation.

fireeyedboy
danilo
Actually you might be right. I added a try-catch-block around the top line of the stacktrace (inside the core files). It did not catch any exception. I created a new issue at the Kohana forum. http://forum.kohanaphp.com/comments.php?DiscussionID=5425Is there a way to catch errors instead of exceptions, to prevent the showing of the standard error page?
danilo
@danilo: Please see my edit.
fireeyedboy
@fireeyedboy: Of course I will disable the display of PHP errors. Also, I am logging errors with Kohana. The problem is, the database connection error (which afaik is not a PHP error) gets caught by the Kohana-internal errorhandling, which displays an error page (just as if an exception got thrown). I don't want that though, I would like to cache my database information, and in case the db access would fail, I would just display the cached information along with a notice that the data isn't up to date.
danilo
@danilo: ah ok, I see now. Well, I'm sorry to say that for that I don't have enough knowledge about Kohana. Hopefully someone else can chime in about this.
fireeyedboy
@fireeyedboy: I guess the core needs a fix there. Thanks anyways.
danilo
Kohana is actually quite consistent with its exceptions. 2.4 has much improved support for stack traces.
Ixmatus
+1  A: 

You can catch the exception, but you are probably trying to catch it in the wrong place. The problem with trying to catch that low-level of an exception is that it can be spawned from many different sources.

For example, if you use the database driver for your sessions that exception will be thrown from instantiation of the database driver in the session library (which is instantiated in a call to session which will probably happen before you instantiate any models).

Catching that exception can happen from the model, but it is more likely to happen from another source - in which case you would probably have to extend a few libraries, or be sure you are wrapping a base model parent::__construct call and the session library in a try-catch block.

(I would personally extend the Model library to do that instead of putting it in a base model)

Hope that helps.

Ixmatus
Thank you... :)
danilo
A: 

Kohana 2 does not convert errors into exceptions. You will either need to attach your own error handler, or use error_reporting() to turn off the error (temporarily) then do some kind of handling yourself.

shadowhand
Thanks. Can I handle the error from the point where i call/use the database connection, or do I need to edit the core/database files?
danilo