From 71a234d67c92dab41c270f407f27e8f6752b575b Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Thu, 6 Jun 2024 19:13:22 +1200 Subject: [PATCH] Refactor exceptions to using `OpenApiException` (#1602) --- src/Analysers/ReflectionAnalyser.php | 3 ++- src/Analysis.php | 6 +++--- src/Annotations/AbstractAnnotation.php | 5 +++-- src/Annotations/OpenApi.php | 9 +++++---- src/Generator.php | 2 +- src/OpenApiException.php | 12 ++++++++++++ src/Pipeline.php | 4 ++-- src/Processors/ExpandEnums.php | 3 ++- src/Serializer.php | 4 ++-- src/Util.php | 4 ++-- 10 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 src/OpenApiException.php diff --git a/src/Analysers/ReflectionAnalyser.php b/src/Analysers/ReflectionAnalyser.php index e94ab872..fb67aa13 100644 --- a/src/Analysers/ReflectionAnalyser.php +++ b/src/Analysers/ReflectionAnalyser.php @@ -10,6 +10,7 @@ use OpenApi\Annotations as OA; use OpenApi\Context; use OpenApi\Generator; +use OpenApi\OpenApiException; /** * OpenApi analyser using reflection. @@ -39,7 +40,7 @@ public function __construct(array $annotationFactories = []) } } if (!$this->annotationFactories) { - throw new \RuntimeException('No suitable annotation factory found. At least one of "Doctrine Annotations" or PHP 8.1 are required'); + throw new OpenApiException('No suitable annotation factory found. At least one of "Doctrine Annotations" or PHP 8.1 are required'); } } diff --git a/src/Analysis.php b/src/Analysis.php index 6ba3a081..e749f8a6 100644 --- a/src/Analysis.php +++ b/src/Analysis.php @@ -364,14 +364,14 @@ public function getContext(object $annotation): ?Context return $annotation->_context; } if ($this->annotations->contains($annotation) === false) { - throw new \Exception('Annotation not found'); + throw new OpenApiException('Annotation not found'); } $context = $this->annotations[$annotation]; if ($context instanceof Context) { return $context; } - throw new \RuntimeException('Annotation has no context - did you use addAnnotation()/addAnnotations()'); + throw new OpenApiException('Annotation has no context - did you use addAnnotation()/addAnnotations()'); } /** @@ -380,7 +380,7 @@ public function getContext(object $annotation): ?Context public function merged(): Analysis { if ($this->openapi === null) { - throw new \Exception('No openapi target set. Run the MergeIntoOpenApi processor'); + throw new OpenApiException('No openapi target set. Run the MergeIntoOpenApi processor'); } $unmerged = $this->openapi->_unmerged; $this->openapi->_unmerged = []; diff --git a/src/Annotations/AbstractAnnotation.php b/src/Annotations/AbstractAnnotation.php index c9bf2f32..e8468549 100644 --- a/src/Annotations/AbstractAnnotation.php +++ b/src/Annotations/AbstractAnnotation.php @@ -9,6 +9,7 @@ use OpenApi\Context; use OpenApi\Generator; use OpenApi\Annotations as OA; +use OpenApi\OpenApiException; use OpenApi\Util; use Symfony\Component\Yaml\Yaml; @@ -549,7 +550,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '', $this->_context->logger->warning($this->identity() . '->' . $property . ' "' . $value . '" is invalid, expecting "' . implode('", "', $type) . '" in ' . $this->_context); } } else { - throw new \Exception('Invalid ' . get_class($this) . '::$_types[' . $property . ']'); + throw new OpenApiException('Invalid ' . get_class($this) . '::$_types[' . $property . ']'); } } $stack[] = $this; @@ -750,7 +751,7 @@ private function validateDefaultTypes(string $type, $value): bool case 'scheme': return in_array($value, ['http', 'https', 'ws', 'wss'], true); default: - throw new \Exception('Invalid type "' . $type . '"'); + throw new OpenApiException('Invalid type "' . $type . '"'); } } diff --git a/src/Annotations/OpenApi.php b/src/Annotations/OpenApi.php index 8a5e55a2..5351a6e8 100644 --- a/src/Annotations/OpenApi.php +++ b/src/Annotations/OpenApi.php @@ -8,6 +8,7 @@ use OpenApi\Analysis; use OpenApi\Generator; +use OpenApi\OpenApiException; use OpenApi\Util; /** @@ -185,7 +186,7 @@ public function saveAs(string $filename, string $format = 'auto'): void } if (file_put_contents($filename, $content) === false) { - throw new \Exception('Failed to saveAs("' . $filename . '", "' . $format . '")'); + throw new OpenApiException('Failed to saveAs("' . $filename . '", "' . $format . '")'); } } @@ -198,7 +199,7 @@ public function ref(string $ref) { if (substr($ref, 0, 2) !== '#/') { // @todo Add support for external (http) refs? - throw new \Exception('Unsupported $ref "' . $ref . '", it should start with "#/"'); + throw new OpenApiException('Unsupported $ref "' . $ref . '", it should start with "#/"'); } return $this->resolveRef($ref, '#/', $this, []); @@ -223,7 +224,7 @@ private static function resolveRef(string $ref, string $resolved, $container, ar if (is_object($container)) { if (property_exists($container, $property) === false) { - throw new \Exception('$ref "' . $ref . '" not found'); + throw new OpenApiException('$ref "' . $ref . '" not found'); } if ($slash === false) { return $container->{$property}; @@ -251,7 +252,7 @@ private static function resolveRef(string $ref, string $resolved, $container, ar } } - throw new \Exception('$ref "' . $unresolved . '" not found'); + throw new OpenApiException('$ref "' . $unresolved . '" not found'); } /** diff --git a/src/Generator.php b/src/Generator.php index ff9c1f75..c59146de 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -102,7 +102,7 @@ function (string $class) use (&$gref): bool { if (!$loaded && $namespace === 'OpenApi\\Annotations\\') { if (in_array(strtolower(substr($class, 20)), ['definition', 'path'])) { // Detected an 2.x annotation? - throw new \Exception('The annotation @SWG\\' . substr($class, 20) . '() is deprecated. Found in ' . Generator::$context . "\nFor more information read the migration guide: https://github.com/zircote/swagger-php/blob/master/docs/Migrating-to-v3.md"); + throw new OpenApiException('The annotation @SWG\\' . substr($class, 20) . '() is deprecated. Found in ' . Generator::$context . "\nFor more information read the migration guide: https://github.com/zircote/swagger-php/blob/master/docs/Migrating-to-v3.md"); } } diff --git a/src/OpenApiException.php b/src/OpenApiException.php new file mode 100644 index 00000000..c3320da3 --- /dev/null +++ b/src/OpenApiException.php @@ -0,0 +1,12 @@ +pipes); if (null === $index || $index < 0 || $index > count($this->pipes)) { - throw new \InvalidArgumentException('Matcher result out of range'); + throw new OpenApiException('Matcher result out of range'); } array_splice($this->pipes, $index, 0, [$pipe]); diff --git a/src/Processors/ExpandEnums.php b/src/Processors/ExpandEnums.php index cbae7a0f..c9199b92 100644 --- a/src/Processors/ExpandEnums.php +++ b/src/Processors/ExpandEnums.php @@ -9,6 +9,7 @@ use OpenApi\Analysis; use OpenApi\Annotations as OA; use OpenApi\Generator; +use OpenApi\OpenApiException; /** * Expands PHP enums. @@ -77,7 +78,7 @@ protected function expandSchemaEnum(Analysis $analysis): void if (is_a($schema->enum, \UnitEnum::class, true)) { $cases = $schema->enum::cases(); } else { - throw new \InvalidArgumentException("Unexpected enum value, requires specifying the Enum class string: $schema->enum"); + throw new OpenApiException("Unexpected enum value, requires specifying the Enum class string: $schema->enum"); } } else { // might be an array of \UnitEnum::class, string, int, etc... diff --git a/src/Serializer.php b/src/Serializer.php index 73946158..ca02e1c4 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -72,7 +72,7 @@ protected static function isValidAnnotationClass(string $className): bool public function deserialize(string $jsonString, string $className): OA\AbstractAnnotation { if (!$this->isValidAnnotationClass($className)) { - throw new \Exception($className . ' is not defined in OpenApi PHP Annotations'); + throw new OpenApiException($className . ' is not defined in OpenApi PHP Annotations'); } return $this->doDeserialize(json_decode($jsonString), $className, new Context(['generated' => true])); @@ -84,7 +84,7 @@ public function deserialize(string $jsonString, string $className): OA\AbstractA public function deserializeFile(string $filename, string $format = 'json', string $className = OA\OpenApi::class): OA\AbstractAnnotation { if (!$this->isValidAnnotationClass($className)) { - throw new \Exception($className . ' is not a valid OpenApi PHP Annotations'); + throw new OpenApiException($className . ' is not a valid OpenApi PHP Annotations'); } $contents = file_get_contents($filename); diff --git a/src/Util.php b/src/Util.php index be427b43..be329af7 100644 --- a/src/Util.php +++ b/src/Util.php @@ -93,7 +93,7 @@ public static function finder($directory, $exclude = null, $pattern = null): Fin } } } else { - throw new \InvalidArgumentException('Unexpected $directory value:' . gettype($directory)); + throw new OpenApiException('Unexpected $directory value:' . gettype($directory)); } if ($exclude !== null) { if (is_string($exclude)) { @@ -103,7 +103,7 @@ public static function finder($directory, $exclude = null, $pattern = null): Fin $finder->notPath(Util::getRelativePath($path, $directory)); } } else { - throw new \InvalidArgumentException('Unexpected $exclude value:' . gettype($exclude)); + throw new OpenApiException('Unexpected $exclude value:' . gettype($exclude)); } }