From 59320f584e7759e3d31fde06f6146ecf9e1f18f0 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 3 Feb 2025 16:13:10 +1100 Subject: [PATCH] Deprecate remaining exceptions Signed-off-by: Kim Pepper --- .../Exceptions/BadMethodCallException.php | 2 +- .../Common/Exceptions/RuntimeException.php | 8 ++- .../Serializer/JsonErrorException.php | 65 ++----------------- .../Exceptions/UnexpectedValueException.php | 8 ++- src/OpenSearch/Endpoints/AbstractEndpoint.php | 2 +- .../Exception/BadMethodCallException.php | 16 +++++ src/OpenSearch/Exception/HttpException.php | 5 +- src/OpenSearch/Exception/JsonException.php | 58 +++++++++++++++++ src/OpenSearch/Exception/RuntimeException.php | 16 +++++ .../Exception/UnexpectedValueException.php | 16 +++++ .../Serializers/SmartSerializer.php | 13 ++-- tests/ClientIntegrationTest.php | 2 +- tests/ClientTest.php | 2 +- tests/Endpoints/AbstractEndpointTest.php | 3 +- tests/Endpoints/CreatePitTest.php | 2 +- tests/Endpoints/CreateTest.php | 2 +- .../Endpoints/RefreshSearchAnalyzersTest.php | 2 +- tests/Endpoints/SqlQueryEndpointTest.php | 2 +- tests/Namespaces/SecurityNamespaceTest.php | 2 +- util/Endpoint.php | 4 +- util/GenerateEndpoints.php | 2 +- 21 files changed, 147 insertions(+), 85 deletions(-) create mode 100644 src/OpenSearch/Exception/BadMethodCallException.php create mode 100644 src/OpenSearch/Exception/JsonException.php create mode 100644 src/OpenSearch/Exception/RuntimeException.php create mode 100644 src/OpenSearch/Exception/UnexpectedValueException.php diff --git a/src/OpenSearch/Common/Exceptions/BadMethodCallException.php b/src/OpenSearch/Common/Exceptions/BadMethodCallException.php index 131c720a7..6e55d6b5c 100644 --- a/src/OpenSearch/Common/Exceptions/BadMethodCallException.php +++ b/src/OpenSearch/Common/Exceptions/BadMethodCallException.php @@ -31,6 +31,6 @@ * * @deprecated in 2.4.0 and will be removed in 3.0.0. */ -class BadMethodCallException extends \BadMethodCallException implements OpenSearchException +class BadMethodCallException extends \OpenSearch\Exception\BadMethodCallException { } diff --git a/src/OpenSearch/Common/Exceptions/RuntimeException.php b/src/OpenSearch/Common/Exceptions/RuntimeException.php index 09ab9af10..03ae23534 100644 --- a/src/OpenSearch/Common/Exceptions/RuntimeException.php +++ b/src/OpenSearch/Common/Exceptions/RuntimeException.php @@ -21,8 +21,12 @@ namespace OpenSearch\Common\Exceptions; -use OpenSearch\Exception\OpenSearchExceptionInterface; +// @phpstan-ignore classConstant.deprecatedClass +@trigger_error(RuntimeException::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\RuntimeException instead.', E_USER_DEPRECATED); -class RuntimeException extends \RuntimeException implements OpenSearchExceptionInterface +/** + * @deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\RuntimeException instead. + */ +class RuntimeException extends \OpenSearch\Exception\RuntimeException { } diff --git a/src/OpenSearch/Common/Exceptions/Serializer/JsonErrorException.php b/src/OpenSearch/Common/Exceptions/Serializer/JsonErrorException.php index 275d3b5d8..79ebf5dc6 100644 --- a/src/OpenSearch/Common/Exceptions/Serializer/JsonErrorException.php +++ b/src/OpenSearch/Common/Exceptions/Serializer/JsonErrorException.php @@ -21,69 +21,14 @@ namespace OpenSearch\Common\Exceptions\Serializer; -use OpenSearch\Exception\OpenSearchExceptionInterface; +// @phpstan-ignore classConstant.deprecatedClass +@trigger_error(JsonErrorException::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\JsonErrorException instead.', E_USER_DEPRECATED); /** * Class JsonErrorException + * + * @deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\JsonErrorException instead. */ -class JsonErrorException extends \Exception implements OpenSearchExceptionInterface +class JsonErrorException extends \OpenSearch\Exception\JsonException { - /** - * @var mixed - */ - private $input; - - /** - * @var mixed - */ - private $result; - - /** - * @var string[] - */ - private static $messages = array( - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', - JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', - JSON_ERROR_SYNTAX => 'Syntax error', - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', - JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', - JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', - JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', - - // JSON_ERROR_* constant values that are available on PHP >= 7.0 - 9 => 'Decoding of value would result in invalid PHP property name', //JSON_ERROR_INVALID_PROPERTY_NAME - 10 => 'Attempted to decode nonexistent UTF-16 code-point' //JSON_ERROR_UTF16 - ); - - /** - * @param mixed $input - * @param mixed $result - */ - public function __construct(int $code, $input, $result, ?\Throwable $previous = null) - { - if (isset(self::$messages[$code]) !== true) { - throw new \InvalidArgumentException(sprintf('Encountered unknown JSON error code: [%d]', $code)); - } - - parent::__construct(self::$messages[$code], $code, $previous); - $this->input = $input; - $this->result = $result; - } - - /** - * @return mixed - */ - public function getInput() - { - return $this->input; - } - - /** - * @return mixed - */ - public function getResult() - { - return $this->result; - } } diff --git a/src/OpenSearch/Common/Exceptions/UnexpectedValueException.php b/src/OpenSearch/Common/Exceptions/UnexpectedValueException.php index 657f1e86e..f149fd66f 100644 --- a/src/OpenSearch/Common/Exceptions/UnexpectedValueException.php +++ b/src/OpenSearch/Common/Exceptions/UnexpectedValueException.php @@ -21,8 +21,12 @@ namespace OpenSearch\Common\Exceptions; -use OpenSearch\Exception\OpenSearchExceptionInterface; +// @phpstan-ignore classConstant.deprecatedClass +@trigger_error(UnexpectedValueException::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\UnexpectedValueException instead.', E_USER_DEPRECATED); -class UnexpectedValueException extends \UnexpectedValueException implements OpenSearchExceptionInterface +/** + * @deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Exception\UnexpectedValueException instead. + */ +class UnexpectedValueException extends \OpenSearch\Exception\UnexpectedValueException { } diff --git a/src/OpenSearch/Endpoints/AbstractEndpoint.php b/src/OpenSearch/Endpoints/AbstractEndpoint.php index 31c19c09e..a490dbaf3 100644 --- a/src/OpenSearch/Endpoints/AbstractEndpoint.php +++ b/src/OpenSearch/Endpoints/AbstractEndpoint.php @@ -21,8 +21,8 @@ namespace OpenSearch\Endpoints; -use OpenSearch\Common\Exceptions\UnexpectedValueException; use OpenSearch\EndpointInterface; +use OpenSearch\Exception\UnexpectedValueException; use OpenSearch\Serializers\SerializerInterface; use function array_filter; diff --git a/src/OpenSearch/Exception/BadMethodCallException.php b/src/OpenSearch/Exception/BadMethodCallException.php new file mode 100644 index 000000000..79198228d --- /dev/null +++ b/src/OpenSearch/Exception/BadMethodCallException.php @@ -0,0 +1,16 @@ +getErrorMessage($code), $code, $previous); + $this->input = $input; + $this->result = $result; + } + + public function getInput(): mixed + { + return $this->input; + } + + public function getResult(): mixed + { + return $this->result; + } + + /** + * Gets the JSON error message for the given error code. + */ + private function getErrorMessage(int $code): string + { + return match ($code) { + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', + JSON_ERROR_SYNTAX => 'Syntax error', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', + JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', + JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', + JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', + + // JSON_ERROR_* constant values that are available on PHP >= 7.0 + 9 => 'Decoding of value would result in invalid PHP property name', //JSON_ERROR_INVALID_PROPERTY_NAME + 10 => 'Attempted to decode nonexistent UTF-16 code-point',//JSON_ERROR_UTF16 + default => throw new \InvalidArgumentException("Encountered unknown JSON error code: [{$code}]"), + }; + } +} diff --git a/src/OpenSearch/Exception/RuntimeException.php b/src/OpenSearch/Exception/RuntimeException.php new file mode 100644 index 000000000..b96ea6e54 --- /dev/null +++ b/src/OpenSearch/Exception/RuntimeException.php @@ -0,0 +1,16 @@ +getCode(), $data, []); + throw new JsonException($e->getCode(), $data, [], $e); } } } diff --git a/tests/ClientIntegrationTest.php b/tests/ClientIntegrationTest.php index b2456c202..bad92229e 100644 --- a/tests/ClientIntegrationTest.php +++ b/tests/ClientIntegrationTest.php @@ -24,9 +24,9 @@ use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\HttpFactory; use OpenSearch\Client; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\EndpointFactory; use OpenSearch\Exception\NotFoundHttpException; +use OpenSearch\Exception\RuntimeException; use OpenSearch\RequestFactory; use OpenSearch\Serializers\SmartSerializer; use OpenSearch\TransportFactory; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 52393e947..4bc25b4ea 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -22,9 +22,9 @@ namespace OpenSearch\Tests; use OpenSearch\Client; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\EndpointFactoryInterface; use OpenSearch\Endpoints\Delete; +use OpenSearch\Exception\RuntimeException; use OpenSearch\TransportInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; diff --git a/tests/Endpoints/AbstractEndpointTest.php b/tests/Endpoints/AbstractEndpointTest.php index 5c28ca69a..98de806fb 100644 --- a/tests/Endpoints/AbstractEndpointTest.php +++ b/tests/Endpoints/AbstractEndpointTest.php @@ -23,6 +23,7 @@ use OpenSearch\EndpointInterface; use OpenSearch\Endpoints\AbstractEndpoint; +use OpenSearch\Exception\UnexpectedValueException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -55,7 +56,7 @@ public function testInvalidParamsCauseErrorsWhenProvidedToSetParams(array $param ->method('getParamWhitelist') ->willReturn(['one', 'two']); - $this->expectException(\OpenSearch\Common\Exceptions\UnexpectedValueException::class); + $this->expectException(UnexpectedValueException::class); $this->endpoint->setParams($params); } diff --git a/tests/Endpoints/CreatePitTest.php b/tests/Endpoints/CreatePitTest.php index 779f68ee6..a55bf4269 100644 --- a/tests/Endpoints/CreatePitTest.php +++ b/tests/Endpoints/CreatePitTest.php @@ -21,8 +21,8 @@ namespace OpenSearch\Tests\Endpoints; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\Endpoints\CreatePit; +use OpenSearch\Exception\RuntimeException; class CreatePitTest extends \PHPUnit\Framework\TestCase { diff --git a/tests/Endpoints/CreateTest.php b/tests/Endpoints/CreateTest.php index c5e7cfd60..9ad1d0379 100644 --- a/tests/Endpoints/CreateTest.php +++ b/tests/Endpoints/CreateTest.php @@ -21,8 +21,8 @@ namespace OpenSearch\Tests\Endpoints; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\Endpoints\Create; +use OpenSearch\Exception\RuntimeException; class CreateTest extends \PHPUnit\Framework\TestCase { diff --git a/tests/Endpoints/RefreshSearchAnalyzersTest.php b/tests/Endpoints/RefreshSearchAnalyzersTest.php index e8e0b6466..70cc68327 100644 --- a/tests/Endpoints/RefreshSearchAnalyzersTest.php +++ b/tests/Endpoints/RefreshSearchAnalyzersTest.php @@ -21,8 +21,8 @@ namespace OpenSearch\Tests\Endpoints; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\Endpoints\Indices\RefreshSearchAnalyzers; +use OpenSearch\Exception\RuntimeException; class RefreshSearchAnalyzersTest extends \PHPUnit\Framework\TestCase { diff --git a/tests/Endpoints/SqlQueryEndpointTest.php b/tests/Endpoints/SqlQueryEndpointTest.php index 793524db6..5f94c4666 100644 --- a/tests/Endpoints/SqlQueryEndpointTest.php +++ b/tests/Endpoints/SqlQueryEndpointTest.php @@ -21,8 +21,8 @@ namespace OpenSearch\Tests\Endpoints; -use OpenSearch\Common\Exceptions\UnexpectedValueException; use OpenSearch\Endpoints\Sql\Query; +use OpenSearch\Exception\UnexpectedValueException; use PHPUnit\Framework\TestCase; /** diff --git a/tests/Namespaces/SecurityNamespaceTest.php b/tests/Namespaces/SecurityNamespaceTest.php index 55c69bcaa..15c0e60dd 100644 --- a/tests/Namespaces/SecurityNamespaceTest.php +++ b/tests/Namespaces/SecurityNamespaceTest.php @@ -15,8 +15,8 @@ namespace OpenSearch\Tests\Namespaces; -use OpenSearch\Common\Exceptions\RuntimeException; use OpenSearch\EndpointFactory; +use OpenSearch\Exception\RuntimeException; use OpenSearch\Namespaces\SecurityNamespace; use OpenSearch\TransportInterface; use PHPUnit\Framework\MockObject\MockObject; diff --git a/util/Endpoint.php b/util/Endpoint.php index 4b7fc1899..008b303e2 100644 --- a/util/Endpoint.php +++ b/util/Endpoint.php @@ -284,7 +284,7 @@ private function extractUrl(array $paths): string $this->name, str_replace(':part', $part, $skeleton) ); - $this->addNamespace('OpenSearch\Common\Exceptions\RuntimeException'); + $this->addNamespace('OpenSearch\Exception\RuntimeException'); } else { $params .= sprintf("%s\$%s = \$this->%s ?? null;", $tab8, $part, $part); } @@ -346,7 +346,7 @@ private function extractUrl(array $paths): string $tab8, $this->apiName ); - $this->addNamespace('OpenSearch\Common\Exceptions\RuntimeException'); + $this->addNamespace('OpenSearch\Exception\RuntimeException'); } return $checkPart . $params . $deprecated . $urls . $else; } diff --git a/util/GenerateEndpoints.php b/util/GenerateEndpoints.php index 4d41fe3b2..35d00c2f6 100644 --- a/util/GenerateEndpoints.php +++ b/util/GenerateEndpoints.php @@ -21,7 +21,7 @@ */ use OpenSearch\Client; use OpenSearch\Common\Exceptions\NoNodesAvailableException; -use OpenSearch\Common\Exceptions\RuntimeException; +use OpenSearch\Exception\RuntimeException; use OpenSearch\Util\ClientEndpoint; use OpenSearch\Util\Endpoint; use OpenSearch\Util\NamespaceEndpoint;