Skip to content

Commit 604ef77

Browse files
committed
PHPLIB-1689 Make the Atlas Search exception more specific
1 parent c9ddfcb commit 604ef77

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
use MongoDB\Driver\Exception\ServerException;
6+
use Throwable;
7+
8+
use function in_array;
9+
10+
final class SearchNotSupportedException extends ServerException
11+
{
12+
public static function isSearchNotSupportedError(Throwable $e): bool
13+
{
14+
if (! $e instanceof ServerException) {
15+
return false;
16+
}
17+
18+
return in_array($e->getCode(), [
19+
59, // MongoDB 4 to 6, 7-community: no such command: 'createSearchIndexes'
20+
40324, // MongoDB 4 to 6: Unrecognized pipeline stage name: '$listSearchIndexes'
21+
115, // MongoDB 7-ent: Search index commands are only supported with Atlas.
22+
6047401, // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas
23+
31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
24+
], true);
25+
}
26+
}

src/Operation/Aggregate.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
use MongoDB\Driver\Command;
2222
use MongoDB\Driver\CursorInterface;
2323
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
24+
use MongoDB\Driver\Exception\ServerException;
2425
use MongoDB\Driver\ReadConcern;
2526
use MongoDB\Driver\ReadPreference;
2627
use MongoDB\Driver\Server;
2728
use MongoDB\Driver\Session;
2829
use MongoDB\Driver\WriteConcern;
2930
use MongoDB\Exception\InvalidArgumentException;
31+
use MongoDB\Exception\SearchNotSupportedException;
3032
use MongoDB\Exception\UnexpectedValueException;
3133
use MongoDB\Exception\UnsupportedException;
3234
use MongoDB\Model\CodecCursor;
@@ -233,7 +235,15 @@ public function execute(Server $server): CursorInterface
233235
$this->createCommandOptions(),
234236
);
235237

236-
$cursor = $this->executeCommand($server, $command);
238+
try {
239+
$cursor = $this->executeCommand($server, $command);
240+
} catch (ServerException $exception) {
241+
if (SearchNotSupportedException::isSearchNotSupportedError($exception)) {
242+
throw new SearchNotSupportedException('Search is not supported by the connected server', $exception->getCode(), $exception);
243+
}
244+
245+
throw $exception;
246+
}
237247

238248
if (isset($this->options['codec'])) {
239249
return CodecCursor::fromCursor($cursor, $this->options['codec']);

0 commit comments

Comments
 (0)