Skip to content

Commit

Permalink
Deprecate remaining exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Pepper <[email protected]>
  • Loading branch information
kimpepper committed Feb 3, 2025
1 parent 92b8e45 commit 59320f5
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
8 changes: 6 additions & 2 deletions src/OpenSearch/Common/Exceptions/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
65 changes: 5 additions & 60 deletions src/OpenSearch/Common/Exceptions/Serializer/JsonErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions src/OpenSearch/Common/Exceptions/UnexpectedValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
2 changes: 1 addition & 1 deletion src/OpenSearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/OpenSearch/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Exception;

use OpenSearch\Common\Exceptions\OpenSearchException;

/**
* An exception thrown when invalid arguments are passed to a method.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class BadMethodCallException extends \BadMethodCallException implements OpenSearchException
{
}
5 changes: 4 additions & 1 deletion src/OpenSearch/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace OpenSearch\Exception;

use OpenSearch\Common\Exceptions\OpenSearchException;

/**
* Exception thrown when an HTTP error occurs.
*
* @phpstan-consistent-constructor
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class HttpException extends \RuntimeException implements HttpExceptionInterface
class HttpException extends \RuntimeException implements HttpExceptionInterface, OpenSearchException
{
public function __construct(
protected readonly int $statusCode,
Expand Down
58 changes: 58 additions & 0 deletions src/OpenSearch/Exception/JsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Exception;

use OpenSearch\Common\Exceptions\OpenSearchException;

/**
* Provides an exception for JSON errors.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class JsonException extends \JsonException implements OpenSearchException
{
private readonly mixed $input;

private readonly mixed $result;

public function __construct(int $code, mixed $input, mixed $result, ?\Throwable $previous = null)
{
parent::__construct($this->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}]"),
};
}
}
16 changes: 16 additions & 0 deletions src/OpenSearch/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Exception;

use OpenSearch\Common\Exceptions\OpenSearchException;

/**
* An exception thrown when a runtime error occurs.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class RuntimeException extends \RuntimeException implements OpenSearchException
{
}
16 changes: 16 additions & 0 deletions src/OpenSearch/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Exception;

use OpenSearch\Common\Exceptions\OpenSearchException;

/**
* An exception thrown when invalid arguments are passed to a method.
*
* @phpstan-ignore class.implementsDeprecatedInterface
*/
class UnexpectedValueException extends \UnexpectedValueException implements OpenSearchException
{
}
13 changes: 6 additions & 7 deletions src/OpenSearch/Serializers/SmartSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace OpenSearch\Serializers;

use OpenSearch\Common\Exceptions;
use OpenSearch\Common\Exceptions\Serializer\JsonErrorException;
use OpenSearch\Exception\JsonException;
use OpenSearch\Exception\RuntimeException;

if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
//PHP < 7.2 Define it as 0 so it does nothing
Expand All @@ -41,7 +41,7 @@ public function serialize($data): string
} else {
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
if ($data === false) {
throw new Exceptions\RuntimeException("Failed to JSON encode: ".json_last_error_msg());
throw new RuntimeException("Failed to JSON encode: ".json_last_error_msg());
}
if ($data === '[]') {
return '{}';
Expand Down Expand Up @@ -70,10 +70,9 @@ public function deserialize(?string $data, array $headers)
}

/**
* @param string|null $data
* Decode JSON data.
*
* @return array
* @throws JsonErrorException
* @throws \OpenSearch\Exception\JsonException
*/
private function decode(?string $data): array
{
Expand All @@ -84,7 +83,7 @@ private function decode(?string $data): array
try {
return json_decode($data, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonErrorException($e->getCode(), $data, []);
throw new JsonException($e->getCode(), $data, [], $e);
}
}
}
2 changes: 1 addition & 1 deletion tests/ClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/Endpoints/AbstractEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OpenSearch\EndpointInterface;
use OpenSearch\Endpoints\AbstractEndpoint;
use OpenSearch\Exception\UnexpectedValueException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Endpoints/CreatePitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Endpoints/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Endpoints/RefreshSearchAnalyzersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Endpoints/SqlQueryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Namespaces/SecurityNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions util/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 59320f5

Please sign in to comment.