Skip to content

Commit 17b1c47

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

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 create(ServerException $e): self
13+
{
14+
return new self('Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. Please connect to Atlas or an AtlasCLI local deployment to enable. For more information on how to connect, see https://dochub.mongodb.org/core/atlas-cli-deploy-local-reqs', $e->getCode(), $e);
15+
}
16+
17+
public static function isSearchNotSupportedError(Throwable $e): bool
18+
{
19+
if (! $e instanceof ServerException) {
20+
return false;
21+
}
22+
23+
return in_array($e->getCode(), [
24+
59, // MongoDB 4 to 6, 7-community: no such command: 'createSearchIndexes'
25+
40324, // MongoDB 4 to 6: Unrecognized pipeline stage name: '$listSearchIndexes'
26+
115, // MongoDB 7-ent: Search index commands are only supported with Atlas.
27+
6047401, // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas
28+
31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
29+
], true);
30+
}
31+
}

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 SearchNotSupportedException::create($exception);
243+
}
244+
245+
throw $exception;
246+
}
237247

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

src/Operation/CreateSearchIndexes.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
use MongoDB\Driver\Command;
2121
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
22+
use MongoDB\Driver\Exception\ServerException;
2223
use MongoDB\Driver\Server;
2324
use MongoDB\Exception\InvalidArgumentException;
25+
use MongoDB\Exception\SearchNotSupportedException;
2426
use MongoDB\Exception\UnsupportedException;
2527
use MongoDB\Model\SearchIndexInput;
2628

@@ -83,7 +85,15 @@ public function execute(Server $server): array
8385
$cmd['comment'] = $this->options['comment'];
8486
}
8587

86-
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
88+
try {
89+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
90+
} catch (ServerException $exception) {
91+
if (SearchNotSupportedException::isSearchNotSupportedError($exception)) {
92+
throw SearchNotSupportedException::create($exception);
93+
}
94+
95+
throw $exception;
96+
}
8797

8898
/** @var object{indexesCreated: list<object{name: string}>} $result */
8999
$result = current($cursor->toArray());

tests/Collection/CollectionFunctionalTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use MongoDB\Driver\ReadPreference;
1414
use MongoDB\Driver\WriteConcern;
1515
use MongoDB\Exception\InvalidArgumentException;
16+
use MongoDB\Exception\SearchNotSupportedException;
1617
use MongoDB\Exception\UnsupportedException;
1718
use MongoDB\Operation\Count;
1819
use MongoDB\Tests\CommandObserver;
@@ -807,6 +808,32 @@ public function testListSearchIndexesInheritTypeMap(): void
807808
$this->assertIsArray($indexes[0]);
808809
}
809810

811+
public function testListSearchIndexesNotSupportedException(): void
812+
{
813+
if (self::isAtlas()) {
814+
self::markTestSkipped('Atlas Search is supported on Atlas');
815+
}
816+
817+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
818+
819+
$this->expectException(SearchNotSupportedException::class);
820+
821+
$collection->listSearchIndexes();
822+
}
823+
824+
public function testCreateSearchIndexNotSupportedException(): void
825+
{
826+
if (self::isAtlas()) {
827+
self::markTestSkipped('Atlas Search is supported on Atlas');
828+
}
829+
830+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
831+
832+
$this->expectException(SearchNotSupportedException::class);
833+
834+
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
835+
}
836+
810837
/**
811838
* Create data fixtures.
812839
*/

0 commit comments

Comments
 (0)