-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle HTTP exceptions in HttpTransport
Signed-off-by: Kim Pepper <[email protected]>
- Loading branch information
Showing
40 changed files
with
566 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Exception; | ||
|
||
/** | ||
* Exception thrown when a 400 Bad Request HTTP error occurs. | ||
*/ | ||
class BadRequestHttpException extends HttpException | ||
{ | ||
public function __construct(string $message = '', array $headers = [], int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct(400, $message, $headers, $code, $previous); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Exception; | ||
|
||
/** | ||
* Exception thrown when a 409 Conflict HTTP error occurs. | ||
*/ | ||
class ConflictHttpException extends HttpException | ||
{ | ||
public function __construct(string $message = '', array $headers = [], int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct(409, $message, $headers, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Exception; | ||
|
||
use OpenSearch\Serializers\SerializerInterface; | ||
|
||
/** | ||
* Extracts an error message from the response body. | ||
*/ | ||
class ErrorMessageExtractor | ||
{ | ||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
) { | ||
} | ||
|
||
public function extractErrorMessage(mixed $body, array $headers): ?string | ||
{ | ||
if (!is_string($body)) { | ||
// Convert non-string response body to string message. | ||
return json_encode($body); | ||
} | ||
|
||
$error = $this->serializer->deserialize($body, $headers); | ||
|
||
if (is_string($error)) { | ||
return $error; | ||
} | ||
|
||
if (!isset($error['error'])) { | ||
// <2.0 "i just blew up" non-structured exception. | ||
// $error is an array, but we don't know the format, so we reuse the response body instead | ||
// added json_encode to convert into a string | ||
return json_encode($body); | ||
} | ||
|
||
// 2.0 structured exceptions | ||
if (is_array($error['error']) && array_key_exists('reason', $error['error'])) { | ||
// Try to use root cause first (only grabs the first root cause) | ||
$info = $error['error']['root_cause'][0] ?? $error['error']; | ||
$cause = $info['reason']; | ||
$type = $info['type']; | ||
|
||
return "$type: $cause"; | ||
} | ||
|
||
// <2.0 semi-structured exceptions | ||
$legacyError = $error['error']; | ||
if (is_array($error['error'])) { | ||
return json_encode($legacyError); | ||
} | ||
return $legacyError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Exception; | ||
|
||
/** | ||
* Exception thrown when a 403 Forbidden HTTP error occurs. | ||
*/ | ||
class ForbiddenHttpException extends HttpException | ||
{ | ||
public function __construct(string $message = '', array $headers = [], int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct(403, $message, $headers, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Exception; | ||
|
||
/** | ||
* Exception thrown when an HTTP error occurs. | ||
* | ||
* @phpstan-consistent-constructor | ||
*/ | ||
class HttpException extends \RuntimeException implements HttpExceptionInterface | ||
{ | ||
public function __construct( | ||
protected readonly int $statusCode, | ||
string $message = '', | ||
protected readonly array $headers = [], | ||
int $code = 0, | ||
?\Throwable $previous = null, | ||
) { | ||
parent::__construct($message, $code, $previous); | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return $this->statusCode; | ||
} | ||
|
||
public function getHeaders(): array | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
} |
Oops, something went wrong.