Skip to content

Commit ccfdcc9

Browse files
authored
Merge pull request #28 from KurtThiemann/master
Add more detailed error messages to OpenSearch exceptions
2 parents db931dc + 3f3da9f commit ccfdcc9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/Driver/OpenSearch/OpenSearchHost.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,38 @@ public function request(string $method, string $uri, mixed $body = null): stdCla
6565

6666
$statusCode = $response->getStatusCode();
6767
if ($statusCode < 200 || $statusCode > 299) {
68-
$parsed = null;
69-
try {
70-
$parsed = $this->parseResponse($response);
71-
} catch (Exception) {}
72-
throw new HttpErrorResponseException($parsed, "OpenSearch returned status code " . $statusCode, $statusCode);
68+
$this->handleErrorResponse($response);
7369
}
7470

7571
return $this->parseResponse($response);
7672
}
7773

74+
/**
75+
* @param ResponseInterface $response
76+
* @return void
77+
* @throws HttpErrorResponseException
78+
*/
79+
protected function handleErrorResponse(ResponseInterface $response): void
80+
{
81+
$statusCode = $response->getStatusCode();
82+
try {
83+
$parsed = $this->parseResponse($response);
84+
} catch (Exception) {
85+
throw new HttpErrorResponseException(null, "OpenSearch returned status code " . $statusCode, $statusCode);
86+
}
87+
88+
$type = $parsed->error?->type ?? null;
89+
$reason = $parsed->error?->reason ?? null;
90+
$message = "OpenSearch returned error " . $statusCode;
91+
if ($type !== null) {
92+
$message .= " [" . $type . "]";
93+
}
94+
if ($reason !== null) {
95+
$message .= " " . $reason;
96+
}
97+
throw new HttpErrorResponseException($parsed, $message, $statusCode);
98+
}
99+
78100
/**
79101
* @param ResponseInterface $response
80102
* @return stdClass

0 commit comments

Comments
 (0)