-
Notifications
You must be signed in to change notification settings - Fork 129
Exception handling #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
26fba08
to
1b64f6d
Compare
As a first start, this seems ok, there are a couple of special cases we can't quite cover yet. For example, inserting a document that violates a unique index normally causes a
The new driver apparently throws a |
fca982d
to
df5c55d
Compare
@alcaeus I have done most of the exception conversion. This got hairy real quick, the exception in the new driver does not make much sense. Anyway, your feedback would be appreciated. |
Thanks, I'll take a look at this soon. Can you list examples where the exceptions don't make sense? Some of the exceptions contain additional information about the error. For example, the |
Actually it's more a semantic issues and things that you won't expect for example trying to connect to a server not responding throw a On the same idea setting |
I haven't looked at stuff yet, but from what I've seen in the driver I'm sure @jmikola et al. had a reason for this ;) |
Probably me, maybe I should read the manual a bit more :-)
|
d5d64ea
to
658bb79
Compare
$indexes = $this->collection->listIndexes(); | ||
foreach ($indexes as $index) { | ||
if ($index->getKey() === $keys) { | ||
throw new \MongoDuplicateKeyException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick test shows that this exception is not actually thrown:
php > $client = new MongoClient();
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(4) {
'createdCollectionAutomatically' =>
bool(true)
'numIndexesBefore' =>
int(1)
'numIndexesAfter' =>
int(2)
'ok' =>
double(1)
}
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(5) {
'createdCollectionAutomatically' =>
bool(false)
'numIndexesBefore' =>
int(2)
'numIndexesAfter' =>
int(2)
'note' =>
string(25) "all indexes already exist"
'ok' =>
double(1)
}
There is an exception if the index already exists with different options:
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1], ['unique' => true]));
PHP Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1
Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd because I have a working test case and I had to catch it in gridfs or the tests would not pass. Maybe because of the ===.
On 2016/01/14, at 3:46, Andreas notifications@github.com wrote:
In lib/Mongo/MongoCollection.php:
-// $expected = [// Note: this is what the result array should look like
-// 'createdCollectionAutomatically' => true,
-// 'numIndexesBefore' => 1,
-// 'numIndexesAfter' => 2,
-// 'ok' => 1.0
-// ];if (! is_array($keys) || ! count($keys)) {
throw new MongoException('keys cannot be empty');
}
// duplicate
$indexes = $this->collection->listIndexes();
foreach ($indexes as $index) {
if ($index->getKey() === $keys) {
A quick test shows that this exception is not actually thrown:throw new \MongoDuplicateKeyException();
php > $client = new MongoClient();
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(4) {
'createdCollectionAutomatically' =>
bool(true)
'numIndexesBefore' =>
int(1)
'numIndexesAfter' =>
int(2)
'ok' =>
double(1)
}
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(5) {
'createdCollectionAutomatically' =>
bool(false)
'numIndexesBefore' =>
int(2)
'numIndexesAfter' =>
int(2)
'note' =>
string(25) "all indexes already exist"
'ok' =>
double(1)
}
There is an exception if the index already exists with different options:php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1], ['unique' => true]));
PHP Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1
―
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I miss read your comment. Ok, I'll modify this to match more closely the real extension.
On 2016/01/14, at 3:46, Andreas notifications@github.com wrote:
In lib/Mongo/MongoCollection.php:
-// $expected = [// Note: this is what the result array should look like
-// 'createdCollectionAutomatically' => true,
-// 'numIndexesBefore' => 1,
-// 'numIndexesAfter' => 2,
-// 'ok' => 1.0
-// ];if (! is_array($keys) || ! count($keys)) {
throw new MongoException('keys cannot be empty');
}
// duplicate
$indexes = $this->collection->listIndexes();
foreach ($indexes as $index) {
if ($index->getKey() === $keys) {
A quick test shows that this exception is not actually thrown:throw new \MongoDuplicateKeyException();
php > $client = new MongoClient();
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(4) {
'createdCollectionAutomatically' =>
bool(true)
'numIndexesBefore' =>
int(1)
'numIndexesAfter' =>
int(2)
'ok' =>
double(1)
}
php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1]));
array(5) {
'createdCollectionAutomatically' =>
bool(false)
'numIndexesBefore' =>
int(2)
'numIndexesAfter' =>
int(2)
'note' =>
string(25) "all indexes already exist"
'ok' =>
double(1)
}
There is an exception if the index already exists with different options:php > var_dump($client->selectCollection('mongo-php-adapter', 'test')->createIndex(['foo' => 1], ['unique' => true]));
PHP Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1Warning: Uncaught exception 'MongoResultException' with message 'localhost:27017: Index with name: foo_1 already exists with different options' in php shell code:1
Stack trace:
#0 php shell code(1): MongoCollection->createIndex(Array, Array)
#1 {main}
thrown in php shell code on line 1
―
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding, this the code is still throwing MongoDuplicateKeyException
rather than MongoResultException
. I kept that way because I don't know if duplicate exception would be ever use and in exactly what situation. If you have the chance to look at the original driver code and tell me. Worst case scenario, I can take a look on my own tomorrow night.
Apart from the comments above, can you rebase this on top of master? There have been changes to |
c73c6c2
to
78c00cf
Compare
@alcaeus I think I have addressed most of your feedback, if you have few minutes to spare to check. |
@@ -772,5 +881,31 @@ private function ensureDocumentHasMongoId(&$document) | |||
|
|||
return null; | |||
} | |||
|
|||
private function validateDocument(&$document) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alcaeus let me know if you think that's overkill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can incorporate these checks into ensureDocumentHasMongoId
since that will return false if the object given is not an object or array. We can just expand those checks to see if there are any empty documents there.
If you have more feedback! |
Looks good to me. Can you rebase the PR and squash some commits (e.g. the "enable failing test" commits)? I'll set aside some time for this in the next couple of days and will then start to stabilize everything in #28. |
c005297
to
736b839
Compare
736b839
to
666dc29
Compare
I have cleaned the branch. Regarding the failing test, it's still the same one. Let me know if I can be any help with #28. |
I've changed the failing test around - I don't have 2.6 (the version running on Travis) around to see the actual error thrown. |
I tried with mongo 2.6 and the test still fails somehow.
|
I actually noticed this today while looking into superfluous warnings emitted during stream initialization. PHPC-581 aims to fix this by throwing ConnectionTimeoutException in this case. We do have a specificity issue with libmongoc, because connections all happen as part of the server selection component. This means that while we might encountered actual socket timeouts or DNS resolution errors internally, this manifests itself as a server selection timeout. We'll hopefully be able to sort this out down the line, but it likely won't happen until libmongoc 1.5 or later.
This doesn't sound correct. If you have a reproducible test case, I'd appreciate it if you could open a JIRA issue for this. |
Try to implement exception converter. I don't think we'll be able to use in 100% of the time but should cover easy situations.
Let me know your feedback.
Reference #7