Skip to content

Commit f295a10

Browse files
fix: add proper error handling with exceptions to Elasticsearch service
Changed all Elasticsearch service methods to throw RuntimeException with detailed error messages instead of silently returning boolean values. This provides better debugging information and ensures failures don't go unnoticed. Changes: - indexDocument() now throws exception with URL and response body on failure - deleteDocument() now throws exception with URL and response body on failure - search() now throws exception with URL and response body on failure - Updated return types from bool to void where appropriate - Added @throws annotations to PHPDoc
1 parent a392fa3 commit f295a10

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/Services/ElasticsearchService.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,41 @@ public function __construct(?string $host = null)
4949
* @param string $index The Elasticsearch index name.
5050
* @param string $id The unique document ID.
5151
* @param array<string, mixed> $body The document body to be stored.
52-
* @return bool True if successful, false otherwise.
52+
* @return void
53+
*
54+
* @throws \RuntimeException if the request fails
5355
*/
54-
public function indexDocument(string $index, string $id, array $body): bool
56+
public function indexDocument(string $index, string $id, array $body): void
5557
{
5658
$url = "{$this->host}/{$index}/_doc/{$id}";
5759
$response = Http::put($url, $body);
5860

59-
return $response->successful();
61+
if (!$response->successful()) {
62+
throw new \RuntimeException(
63+
"Failed to index document to Elasticsearch [{$url}]: " . $response->body()
64+
);
65+
}
6066
}
6167

6268
/**
6369
* Delete a document from Elasticsearch by its ID.
6470
*
6571
* @param string $index The Elasticsearch index name.
6672
* @param string $id The document ID to delete.
67-
* @return bool True if successful, false otherwise.
73+
* @return void
74+
*
75+
* @throws \RuntimeException if the request fails
6876
*/
69-
public function deleteDocument(string $index, string $id): bool
77+
public function deleteDocument(string $index, string $id): void
7078
{
7179
$url = "{$this->host}/{$index}/_doc/{$id}";
7280
$response = Http::delete($url);
7381

74-
return $response->successful();
82+
if (!$response->successful()) {
83+
throw new \RuntimeException(
84+
"Failed to delete document from Elasticsearch [{$url}]: " . $response->body()
85+
);
86+
}
7587
}
7688

7789
/**
@@ -80,12 +92,20 @@ public function deleteDocument(string $index, string $id): bool
8092
* @param string $index The Elasticsearch index name.
8193
* @param array<string, mixed> $query The Elasticsearch query body.
8294
* @return array<int, mixed> The array of matching documents (hits).
95+
*
96+
* @throws \RuntimeException if the request fails
8397
*/
8498
public function search(string $index, array $query): array
8599
{
86100
$url = "{$this->host}/{$index}/_search";
87101
$response = Http::post($url, $query);
88102

103+
if (!$response->successful()) {
104+
throw new \RuntimeException(
105+
"Failed to search Elasticsearch [{$url}]: " . $response->body()
106+
);
107+
}
108+
89109
return $response->json('hits.hits', []);
90110
}
91111
}

0 commit comments

Comments
 (0)