Skip to content

Commit

Permalink
handle 404 exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Nov 7, 2024
1 parent 8eed37e commit b1773c9
Showing 1 changed file with 58 additions and 87 deletions.
145 changes: 58 additions & 87 deletions src/SearchClient/SearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\ElasticsearchClientBundle\SearchClient;

use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Exception;
use Pimcore\SearchClient\Exception\ClientException;
Expand Down Expand Up @@ -44,9 +45,7 @@ public function create(array $params): array
try {
return $this->getArrayResponse($this->client->create($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to create data: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to create data');
}
}

Expand All @@ -58,9 +57,7 @@ public function search(array $params): array
try {
return $this->getArrayResponse($this->client->search($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to search data: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to search data');
}
}

Expand All @@ -72,9 +69,7 @@ public function get(array $params): array
try {
return $this->getArrayResponse($this->client->get($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get data: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get data');
}
}

Expand All @@ -86,9 +81,7 @@ public function exists(array $params): bool
try {
return $this->getBoolResponse($this->client->exists($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to check if data exists: %s', $exception->getMessage())
);
return $this->handleExceptionsWithBool($exception, 'Failed to check if data exists');
}
}

Expand All @@ -100,9 +93,7 @@ public function count(array $params): array
try {
return $this->getArrayResponse($this->client->count($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to count data: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to count data');
}
}

Expand All @@ -114,9 +105,7 @@ public function index(array $params): array
try {
return $this->getArrayResponse($this->client->index($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Index operation failed: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Index operation failed');
}
}

Expand All @@ -128,9 +117,7 @@ public function bulk(array $params): array
try {
return $this->getArrayResponse($this->client->bulk($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Bulk operation failed: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Bulk operation failed');
}
}

Expand All @@ -142,9 +129,7 @@ public function delete(array $params): array
try {
return $this->getArrayResponse($this->client->delete($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Delete operation failed: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Delete operation failed');
}
}

Expand All @@ -156,9 +141,7 @@ public function updateByQuery(array $params): array
try {
return $this->getArrayResponse($this->client->updateByQuery($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to update by query: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to update by query');
}
}

Expand All @@ -170,9 +153,7 @@ public function deleteByQuery(array $params): array
try {
return $this->getArrayResponse($this->client->updateByQuery($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to delete by query: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to delete by query');
}
}

Expand All @@ -184,9 +165,7 @@ public function createIndex(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->create($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to create index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to create index');
}
}

Expand All @@ -198,9 +177,7 @@ public function openIndex(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->create($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to open index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to open index');
}
}

Expand All @@ -212,9 +189,7 @@ public function closeIndex(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->create($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to close index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to close index');
}
}

Expand All @@ -226,9 +201,7 @@ public function getAllIndices(array $params): array
try {
return $this->getArrayResponse($this->client->cat()->indices($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get all indices: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get all indices');
}
}

Expand All @@ -240,9 +213,7 @@ public function existsIndex(array $params): bool
try {
return $this->getBoolResponse($this->client->indices()->exists($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to check if index exists: %s', $exception->getMessage())
);
return $this->handleExceptionsWithBool($exception, 'Failed to check if index exists');
}
}

Expand All @@ -254,9 +225,7 @@ public function reIndex(array $params): array
try {
return $this->getArrayResponse($this->client->reindex($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to reindex: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to reindex');
}
}

Expand All @@ -268,9 +237,7 @@ public function refreshIndex(array $params = []): array
try {
return $this->getArrayResponse($this->client->indices()->refresh($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to refresh index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to refresh index');
}
}

Expand All @@ -282,9 +249,7 @@ public function flushIndex(array $params = []): array
try {
return $this->getArrayResponse($this->client->indices()->flush($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to flush index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to flush index');
}
}

Expand All @@ -296,9 +261,7 @@ public function deleteIndex(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->delete($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to delete an index: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to delete index');
}
}

Expand All @@ -310,9 +273,7 @@ public function existsIndexAlias(array $params): bool
try {
return $this->getBoolResponse($this->client->indices()->existsAlias($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to check if Alias exists: %s', $exception->getMessage())
);
return $this->handleExceptionsWithBool($exception, 'Failed to check if Alias exists');
}
}

Expand All @@ -324,9 +285,7 @@ public function getIndexAlias(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->getAlias($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get an Alias: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get an Alias');
}
}

Expand All @@ -338,9 +297,7 @@ public function deleteIndexAlias(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->deleteAlias($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to delete an Alias: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to delete an Alias');
}
}

Expand All @@ -352,9 +309,7 @@ public function getAllIndexAliases(array $params): array
try {
return $this->getArrayResponse($this->client->cat()->aliases($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get all index Aliases: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get all index Aliases');
}
}

Expand All @@ -366,9 +321,7 @@ public function updateIndexAliases(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->updateAliases($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to update Aliases: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to update Aliases');
}
}

Expand All @@ -380,9 +333,7 @@ public function putIndexMapping(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->putMapping($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to put Mapping: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to put mapping');
}
}

Expand All @@ -394,9 +345,7 @@ public function getIndexMapping(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->getMapping($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get Mapping: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get mapping');
}
}

Expand All @@ -408,9 +357,7 @@ public function getIndexSettings(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->getSettings($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get index settings: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get index settings');
}
}

Expand All @@ -422,9 +369,7 @@ public function putIndexSettings(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->putSettings($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to update index settings: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to update index settings');
}
}

Expand All @@ -436,12 +381,38 @@ public function getIndexStats(array $params): array
try {
return $this->getArrayResponse($this->client->indices()->stats($params));
} catch (Exception $exception) {
throw new ClientException(
sprintf('Failed to get index stats: %s', $exception->getMessage())
);
return $this->handleExceptionsWithArray($exception, 'Failed to get index stats');
}
}

/**
* @throws ClientException
*/
private function handleExceptionsWIthBool(Exception $exception, string $message): bool
{
if ($exception instanceof ClientResponseException && $exception->getCode() === 404) {
return false;
}

throw new ClientException(
sprintf('%s :%s', $message, $exception->getMessage())
);
}

/**
* @throws ClientException
*/
private function handleExceptionsWithArray(Exception $exception, string $message): array
{
if ($exception instanceof ClientResponseException && $exception->getCode() === 404) {
return [];
}

throw new ClientException(
sprintf('%s :%s', $message, $exception->getMessage())
);
}

private function getArrayResponse(Elasticsearch $response): array
{
return $response->asArray();
Expand Down

0 comments on commit b1773c9

Please sign in to comment.