purekid / mongodm

MongoDB ORM that includes support for references,embed and multilevel inheritance.
MIT License
200 stars 47 forks source link

Inconsistent behavior when Mongo is not running #81

Closed willianpts closed 9 years ago

willianpts commented 9 years ago

With MongoDB stopped mongodm throws an exception, which is expected. But if we try to save something again, it crashes:

PHP Fatal error: Call to a member function selectCollection() on null in /home/willian/htdocs/test/vendor/purekid/mongodm/src/Purekid/Mongodm/MongoDB.php on line 708

<?php
require 'vendor/autoload.php';

\Purekid\Mongodm\MongoDB::setConfigBlock('default', array(
    'connection' => array(
        'hostnames' => 'localhost',
        'database'  => 'test'
    )
));

class Test extends \Purekid\Mongodm\Model {
    protected static $collection = "test";
}

$record = new Test();

try {
    $record->save();
}
catch (\Exception $e) {
    echo 'Catch exception #1 ', PHP_EOL;
}

try {
    $record->save();
}
catch (\Exception $e) {
    //
    echo 'Catch exception #2 ', PHP_EOL;
}
jrschumacher commented 9 years ago

Ahh it looks like it kills the MongoDB handle after it encounters the first exception.

jrschumacher commented 9 years ago

@willianpts this fix will change the response to

Catch exception #1 Unable to connect to MongoDB server at mongodb://localhost
Catch exception #2 Failed to connect to: localhost:27017: Previous connection attempts failed, server blacklisted

The second message is from MongoDB. What's your thought about leaving it as is? Should it be caught and converted to similar format as Exception 1?

willianpts commented 9 years ago

The first exception is an instance of \Exception and the subsequent exceptions are \MongoConnectionException.

I think It would be appropriate if all the exceptions were \MongoConnectionException, since this is documented http://php.net/manual/en/class.mongoconnectionexception.php