From 61cdfabc4b9926ac6816473bc9b532a7367411a2 Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Mon, 16 Jun 2025 13:51:31 +1000 Subject: [PATCH 1/6] Catch ClientExceptionInterface in BackendClient --- src/SearchAPI/BackendClient.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 69967f6..808708e 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -19,6 +19,7 @@ use Drupal\search_api_opensearch\SearchAPI\Query\QueryResultParser; use OpenSearch\Client; use OpenSearch\Common\Exceptions\OpenSearchException; use OpenSearch\Exception\OpenSearchExceptionInterface; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\NetworkExceptionInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -121,7 +122,7 @@ class BackendClient implements BackendClientInterface { throw new SearchApiException('An error occurred indexing items.'); } } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('%s when indexing items in index %s.', $e->getMessage(), $indexId), 0, $e); } @@ -142,7 +143,7 @@ class BackendClient implements BackendClientInterface { try { $this->client->bulk($params); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred deleting items from the index %s.', $indexId), 0, $e); } @@ -165,7 +166,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } } - catch (NetworkExceptionInterface $e) { + catch (NetworkExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Network error: %s', $e->getMessage()), 0, $e); } @@ -184,7 +185,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Error querying index %s', $indexId), 0, $e); } } @@ -202,7 +203,7 @@ class BackendClient implements BackendClientInterface { 'index' => [$indexId], ]); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred removing the index %s.', $indexId), 0, $e); } } @@ -225,7 +226,7 @@ class BackendClient implements BackendClientInterface { $event = new IndexCreatedEvent($index); $this->eventDispatcher->dispatch($event); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred creating the index %s.', $indexId), 0, $e); } } @@ -265,7 +266,7 @@ class BackendClient implements BackendClientInterface { $params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index); $this->client->indices()->putMapping($params); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e); } } @@ -288,7 +289,7 @@ class BackendClient implements BackendClientInterface { 'index' => $indexId, ]); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred checking if the index %s exists.', $indexId), 0, $e); } } @@ -361,7 +362,7 @@ class BackendClient implements BackendClientInterface { 'body' => $settings, ]); } - catch (OpenSearchException $e) { + catch (OpenSearchException|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating settings for index %s.', $indexId), 0, $e); } finally { @@ -389,7 +390,7 @@ class BackendClient implements BackendClientInterface { 'index' => $this->getIndexId($index), ]); } - catch (OpenSearchException) { + catch (OpenSearchException|ClientExceptionInterface) { // If we can't get mappings for some reason, then return early. return TRUE; } -- GitLab From b9fcfd196c76ed9fe8ae00d73f1322a0cfae5b17 Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Mon, 16 Jun 2025 14:19:15 +1000 Subject: [PATCH 2/6] Spaces --- src/SearchAPI/BackendClient.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 808708e..44f5711 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -122,7 +122,7 @@ class BackendClient implements BackendClientInterface { throw new SearchApiException('An error occurred indexing items.'); } } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('%s when indexing items in index %s.', $e->getMessage(), $indexId), 0, $e); } @@ -143,7 +143,7 @@ class BackendClient implements BackendClientInterface { try { $this->client->bulk($params); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred deleting items from the index %s.', $indexId), 0, $e); } @@ -203,7 +203,7 @@ class BackendClient implements BackendClientInterface { 'index' => [$indexId], ]); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred removing the index %s.', $indexId), 0, $e); } } @@ -226,7 +226,7 @@ class BackendClient implements BackendClientInterface { $event = new IndexCreatedEvent($index); $this->eventDispatcher->dispatch($event); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred creating the index %s.', $indexId), 0, $e); } } @@ -266,7 +266,7 @@ class BackendClient implements BackendClientInterface { $params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index); $this->client->indices()->putMapping($params); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e); } } @@ -289,7 +289,7 @@ class BackendClient implements BackendClientInterface { 'index' => $indexId, ]); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred checking if the index %s exists.', $indexId), 0, $e); } } @@ -362,7 +362,7 @@ class BackendClient implements BackendClientInterface { 'body' => $settings, ]); } - catch (OpenSearchException|ClientExceptionInterface $e) { + catch (OpenSearchException | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating settings for index %s.', $indexId), 0, $e); } finally { @@ -390,7 +390,7 @@ class BackendClient implements BackendClientInterface { 'index' => $this->getIndexId($index), ]); } - catch (OpenSearchException|ClientExceptionInterface) { + catch (OpenSearchException | ClientExceptionInterface) { // If we can't get mappings for some reason, then return early. return TRUE; } -- GitLab From 45566e36d0db3bd860f5567a373e9e641edc2f40 Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Mon, 16 Jun 2025 14:23:12 +1000 Subject: [PATCH 3/6] More spaces --- src/SearchAPI/BackendClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 44f5711..85a69d9 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -166,7 +166,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } } - catch (NetworkExceptionInterface|ClientExceptionInterface $e) { + catch (NetworkExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Network error: %s', $e->getMessage()), 0, $e); } @@ -185,7 +185,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Error querying index %s', $indexId), 0, $e); } } -- GitLab From bf4e82bec1cfe62e971e7821494e7434662fc17a Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Tue, 17 Jun 2025 09:51:47 +1000 Subject: [PATCH 4/6] Catch OpenSearchExceptionInterface --- src/SearchAPI/BackendClient.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 85a69d9..88feeed 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -17,9 +17,7 @@ use Drupal\search_api_opensearch\Event\IndexCreatedEvent; use Drupal\search_api_opensearch\SearchAPI\Query\QueryParamBuilder; use Drupal\search_api_opensearch\SearchAPI\Query\QueryResultParser; use OpenSearch\Client; -use OpenSearch\Common\Exceptions\OpenSearchException; use OpenSearch\Exception\OpenSearchExceptionInterface; -use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\NetworkExceptionInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -122,7 +120,7 @@ class BackendClient implements BackendClientInterface { throw new SearchApiException('An error occurred indexing items.'); } } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('%s when indexing items in index %s.', $e->getMessage(), $indexId), 0, $e); } @@ -143,7 +141,7 @@ class BackendClient implements BackendClientInterface { try { $this->client->bulk($params); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred deleting items from the index %s.', $indexId), 0, $e); } @@ -166,7 +164,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } } - catch (NetworkExceptionInterface | ClientExceptionInterface $e) { + catch (NetworkExceptionInterface $e) { throw new SearchApiException(sprintf('Network error: %s', $e->getMessage()), 0, $e); } @@ -185,7 +183,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } - catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('Error querying index %s', $indexId), 0, $e); } } @@ -203,7 +201,7 @@ class BackendClient implements BackendClientInterface { 'index' => [$indexId], ]); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred removing the index %s.', $indexId), 0, $e); } } @@ -226,7 +224,7 @@ class BackendClient implements BackendClientInterface { $event = new IndexCreatedEvent($index); $this->eventDispatcher->dispatch($event); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred creating the index %s.', $indexId), 0, $e); } } @@ -266,7 +264,7 @@ class BackendClient implements BackendClientInterface { $params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index); $this->client->indices()->putMapping($params); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e); } } @@ -289,7 +287,7 @@ class BackendClient implements BackendClientInterface { 'index' => $indexId, ]); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred checking if the index %s exists.', $indexId), 0, $e); } } @@ -362,7 +360,7 @@ class BackendClient implements BackendClientInterface { 'body' => $settings, ]); } - catch (OpenSearchException | ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating settings for index %s.', $indexId), 0, $e); } finally { @@ -390,7 +388,7 @@ class BackendClient implements BackendClientInterface { 'index' => $this->getIndexId($index), ]); } - catch (OpenSearchException | ClientExceptionInterface) { + catch (OpenSearchExceptionInterface) { // If we can't get mappings for some reason, then return early. return TRUE; } -- GitLab From e44cc2fdf1dab3dbc0cb9b5b04e2c1e383214d45 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Tue, 17 Jun 2025 14:23:42 +1000 Subject: [PATCH 5/6] Catch both OpenSearchExceptionInterface and ClientExceptionInterface --- src/SearchAPI/BackendClient.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 88feeed..2310b92 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -18,7 +18,7 @@ use Drupal\search_api_opensearch\SearchAPI\Query\QueryParamBuilder; use Drupal\search_api_opensearch\SearchAPI\Query\QueryResultParser; use OpenSearch\Client; use OpenSearch\Exception\OpenSearchExceptionInterface; -use Psr\Http\Client\NetworkExceptionInterface; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -120,7 +120,7 @@ class BackendClient implements BackendClientInterface { throw new SearchApiException('An error occurred indexing items.'); } } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('%s when indexing items in index %s.', $e->getMessage(), $indexId), 0, $e); } @@ -141,7 +141,7 @@ class BackendClient implements BackendClientInterface { try { $this->client->bulk($params); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred deleting items from the index %s.', $indexId), 0, $e); } @@ -164,7 +164,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } } - catch (NetworkExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Network error: %s', $e->getMessage()), 0, $e); } @@ -183,7 +183,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Error querying index %s', $indexId), 0, $e); } } @@ -201,7 +201,7 @@ class BackendClient implements BackendClientInterface { 'index' => [$indexId], ]); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred removing the index %s.', $indexId), 0, $e); } } @@ -224,7 +224,7 @@ class BackendClient implements BackendClientInterface { $event = new IndexCreatedEvent($index); $this->eventDispatcher->dispatch($event); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred creating the index %s.', $indexId), 0, $e); } } @@ -264,7 +264,7 @@ class BackendClient implements BackendClientInterface { $params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index); $this->client->indices()->putMapping($params); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e); } } @@ -287,7 +287,7 @@ class BackendClient implements BackendClientInterface { 'index' => $indexId, ]); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred checking if the index %s exists.', $indexId), 0, $e); } } @@ -360,7 +360,7 @@ class BackendClient implements BackendClientInterface { 'body' => $settings, ]); } - catch (OpenSearchExceptionInterface $e) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating settings for index %s.', $indexId), 0, $e); } finally { @@ -388,7 +388,7 @@ class BackendClient implements BackendClientInterface { 'index' => $this->getIndexId($index), ]); } - catch (OpenSearchExceptionInterface) { + catch (OpenSearchExceptionInterface|ClientExceptionInterface) { // If we can't get mappings for some reason, then return early. return TRUE; } -- GitLab From 2f476bb2a477b11dd40be81d0c8a3d9bbbfe0174 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Tue, 17 Jun 2025 14:33:24 +1000 Subject: [PATCH 6/6] Fix phpcs --- src/SearchAPI/BackendClient.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/SearchAPI/BackendClient.php b/src/SearchAPI/BackendClient.php index 2310b92..a3c12f0 100644 --- a/src/SearchAPI/BackendClient.php +++ b/src/SearchAPI/BackendClient.php @@ -120,7 +120,7 @@ class BackendClient implements BackendClientInterface { throw new SearchApiException('An error occurred indexing items.'); } } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('%s when indexing items in index %s.', $e->getMessage(), $indexId), 0, $e); } @@ -141,7 +141,7 @@ class BackendClient implements BackendClientInterface { try { $this->client->bulk($params); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred deleting items from the index %s.', $indexId), 0, $e); } @@ -164,7 +164,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Network error: %s', $e->getMessage()), 0, $e); } @@ -183,7 +183,7 @@ class BackendClient implements BackendClientInterface { return $resultSet; } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('Error querying index %s', $indexId), 0, $e); } } @@ -201,7 +201,7 @@ class BackendClient implements BackendClientInterface { 'index' => [$indexId], ]); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred removing the index %s.', $indexId), 0, $e); } } @@ -224,7 +224,7 @@ class BackendClient implements BackendClientInterface { $event = new IndexCreatedEvent($index); $this->eventDispatcher->dispatch($event); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred creating the index %s.', $indexId), 0, $e); } } @@ -264,7 +264,7 @@ class BackendClient implements BackendClientInterface { $params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index); $this->client->indices()->putMapping($params); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e); } } @@ -287,7 +287,7 @@ class BackendClient implements BackendClientInterface { 'index' => $indexId, ]); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred checking if the index %s exists.', $indexId), 0, $e); } } @@ -360,7 +360,7 @@ class BackendClient implements BackendClientInterface { 'body' => $settings, ]); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface $e) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface $e) { throw new SearchApiException(sprintf('An error occurred updating settings for index %s.', $indexId), 0, $e); } finally { @@ -388,7 +388,7 @@ class BackendClient implements BackendClientInterface { 'index' => $this->getIndexId($index), ]); } - catch (OpenSearchExceptionInterface|ClientExceptionInterface) { + catch (OpenSearchExceptionInterface | ClientExceptionInterface) { // If we can't get mappings for some reason, then return early. return TRUE; } -- GitLab