Skip to content

Commit fca982d

Browse files
author
Olivier Lechevalier
committed
Add exceptions to MongoCursor
1 parent 95872ab commit fca982d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Mongo/MongoCursor.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
use Alcaeus\MongoDbAdapter\AbstractCursor;
17+
use Alcaeus\MongoDbAdapter\ExceptionConverter;
1718
use MongoDB\Driver\Cursor;
1819
use MongoDB\Driver\ReadPreference;
1920
use MongoDB\Operation\Find;
@@ -139,8 +140,12 @@ public function count($foundOnly = false)
139140
}
140141

141142
$options = $this->getOptions($optionNames) + $this->options;
143+
try {
144+
$count = $this->collection->count($this->query, $options);
145+
} catch (\MongoDB\Driver\Exception\Exception $e) {
146+
ExceptionConverter::toLegacy($e);
147+
}
142148

143-
$count = $this->collection->count($this->query, $options);
144149
return $count;
145150
}
146151

@@ -154,7 +159,12 @@ protected function doQuery()
154159
{
155160
$options = $this->getOptions() + $this->options;
156161

157-
$this->cursor = $this->collection->find($this->query, $options);
162+
try {
163+
$this->cursor = $this->collection->find($this->query, $options);
164+
} catch (\MongoDB\Driver\Exception\Exception $e) {
165+
ExceptionConverter::toLegacy($e);
166+
}
167+
158168
}
159169

160170
/**

tests/Alcaeus/MongoDbAdapter/MongoCursorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public function testCount()
3838
$this->assertSame(1, $cursor->count(true));
3939
}
4040

41+
public function testCountCannotConnect()
42+
{
43+
$client = $this->getClient([], 'mongodb://localhost:28888');
44+
$cursor = $client->selectCollection('mongo-php-adapter', 'test')->find();
45+
46+
$this->setExpectedException('MongoConnectionException');
47+
48+
$cursor->count();
49+
}
50+
4151
/**
4252
* @dataProvider getCursorOptions
4353
*/

0 commit comments

Comments
 (0)