Skip to content

Commit

Permalink
Refactor exceptions to using OpenApiException (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Jun 6, 2024
1 parent b1a792e commit 71a234d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
use OpenApi\OpenApiException;

/**
* OpenApi analyser using reflection.
Expand Down Expand Up @@ -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');
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()');
}

/**
Expand All @@ -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 = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 . '"');
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/Annotations/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OpenApi\Analysis;
use OpenApi\Generator;
use OpenApi\OpenApiException;
use OpenApi\Util;

/**
Expand Down Expand Up @@ -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 . '")');
}
}

Expand All @@ -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, []);
Expand All @@ -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};
Expand Down Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/OpenApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi;

class OpenApiException extends \Exception
{

}
4 changes: 2 additions & 2 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function add(callable $pipe): Pipeline
public function remove($pipe = null, ?callable $matcher = null): Pipeline
{
if (!$pipe && !$matcher) {
throw new \InvalidArgumentException('pipe or callable must not be empty');
throw new OpenApiException('pipe or callable must not be empty');
}

// allow matching on class name in $pipe in a string
Expand Down Expand Up @@ -79,7 +79,7 @@ public function insert(callable $pipe, callable $matcher): Pipeline
{
$index = $matcher($this->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]);
Expand Down
3 changes: 2 additions & 1 deletion src/Processors/ExpandEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use OpenApi\OpenApiException;

/**
* Expands PHP enums.
Expand Down Expand Up @@ -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...
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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));
}
}

Expand Down

0 comments on commit 71a234d

Please sign in to comment.