Skip to content

Commit 10b0c1a

Browse files
committed
Query exception bug
1 parent 887588f commit 10b0c1a

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/Exceptions/QueryException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ private function routeClientResponseException($result): string
6060
return $this->formatAsConflictException($error);
6161
}
6262

63+
if (! isset($error['error']['type'])) {
64+
return $result->getMessage();
65+
}
66+
6367
return match ($error['error']['type']) {
6468
'search_phase_execution_exception' => $this->formatSearchPhaseExecutionException($error),
6569
'script_exception' => $this->formatScriptException($error),

tests/ExceptionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,26 @@
1919
2020
Root Cause Type: parsing_exception
2121
Root Cause Reason: Unknown key for a START_ARRAY in [query]');
22+
23+
it('handles error responses without error.type structure', function () {
24+
$body = json_encode(['status' => 400, 'message' => 'Something went wrong']);
25+
$response = new \GuzzleHttp\Psr7\Response(400, [], $body);
26+
$previous = new \Elastic\Elasticsearch\Exception\ClientResponseException($body, 400);
27+
$previous->setResponse($response);
28+
29+
$exception = new QueryException($previous);
30+
31+
expect($exception)->toBeInstanceOf(QueryException::class)
32+
->and($exception->getMessage())->toBe($body);
33+
});
34+
35+
it('handles error responses with null json body', function () {
36+
$response = new \GuzzleHttp\Psr7\Response(400, [], 'not json at all');
37+
$previous = new \Elastic\Elasticsearch\Exception\ClientResponseException('not json at all', 400);
38+
$previous->setResponse($response);
39+
40+
$exception = new QueryException($previous);
41+
42+
expect($exception)->toBeInstanceOf(QueryException::class)
43+
->and($exception->getMessage())->toBe('not json at all');
44+
});

0 commit comments

Comments
 (0)