Skip to content

Commit f51203e

Browse files
JacquesDurandsoyuka
authored andcommitted
fix(review): throw instead of logging
1 parent 6693402 commit f51203e

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\ApiResource;
2222
use ApiPlatform\Metadata\CollectionOperationInterface;
2323
use ApiPlatform\Metadata\Error;
24+
use ApiPlatform\Metadata\Exception\RuntimeException;
2425
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
2526
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
2627
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
@@ -55,7 +56,6 @@
5556
use ApiPlatform\OpenApi\Serializer\NormalizeOperationNameTrait;
5657
use ApiPlatform\State\Pagination\PaginationOptions;
5758
use Psr\Container\ContainerInterface;
58-
use Psr\Log\LoggerInterface;
5959
use Symfony\Component\PropertyInfo\Type;
6060
use Symfony\Component\Routing\RouteCollection;
6161
use Symfony\Component\Routing\RouterInterface;
@@ -93,7 +93,6 @@ public function __construct(
9393
?Options $openApiOptions = null,
9494
?PaginationOptions $paginationOptions = null,
9595
private readonly ?RouterInterface $router = null,
96-
private readonly ?LoggerInterface $logger = null
9796
) {
9897
$this->filterLocator = $filterLocator;
9998
$this->openApiOptions = $openApiOptions ?: new Options('API Platform');
@@ -883,39 +882,32 @@ private function addOperationErrors(Operation $operation, array $errors, array $
883882
$existingResponses = null;
884883
foreach ($errors as $error) {
885884
if (!is_a($error, ProblemExceptionInterface::class, true)) {
886-
$this->logger?->warning(\sprintf('The error class "%s" does not implement "%s". Did you forget a use statement?', $error, ProblemExceptionInterface::class));
885+
throw new RuntimeException(\sprintf('The error class "%s" does not implement "%s". Did you forget a use statement?', $error, ProblemExceptionInterface::class));
887886
}
888887

889888
$status = null;
890889
$description = null;
890+
891891
try {
892-
/** @var ProblemExceptionInterface */
893-
$exception = (new \ReflectionClass($error))->newInstanceWithoutConstructor();
892+
/** @var ProblemExceptionInterface $exception */
893+
$exception = new $error;
894894
$status = $exception->getStatus();
895895
$description = $exception->getTitle();
896-
} catch (\ReflectionException) {
897-
}
896+
} catch (\TypeError) {}
898897

899898
try {
900899
$errorOperation = $this->resourceMetadataFactory->create($error)->getOperation();
901900
if (!is_a($errorOperation, Error::class)) {
902-
$this->logger?->warning(\sprintf('The error class %s is not an ErrorResource', $error));
903-
continue;
901+
throw new RuntimeException(\sprintf('The error class %s is not an ErrorResource', $error));
904902
}
905-
/* @var Error $errorOperation */
906-
$status ??= $errorOperation->getStatus();
907-
$description ??= $errorOperation->getDescription();
908903
} catch (ResourceClassNotFoundException|OperationNotFoundException) {
909-
$this->logger?->warning(\sprintf('The error class %s is not an ErrorResource', $error));
910-
continue;
904+
$errorOperation = null;
911905
}
906+
$status ??= $errorOperation?->getStatus();
907+
$description ??= $errorOperation?->getDescription();
912908

913909
if (!$status) {
914-
$this->logger?->error(\sprintf(
915-
'The error class %s has no status defined, please either implement ProblemExceptionInterface, or make it an ErrorResource with a status',
916-
$error
917-
));
918-
continue;
910+
throw new RuntimeException(\sprintf('The error class %s has no status defined, please either implement ProblemExceptionInterface, or make it an ErrorResource with a status', $error));
919911
}
920912

921913
$operationErrorSchemas = [];

src/Symfony/Bundle/Resources/config/openapi.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
<argument type="service" id="api_platform.openapi.options" />
9292
<argument type="service" id="api_platform.pagination_options" />
9393
<argument type="service" id="api_platform.router" />
94-
<argument type="service" id="logger" on-invalid="null" />
9594
</service>
9695

9796
<service id="api_platform.jsonopenapi.encoder" class="ApiPlatform\Serializer\JsonEncoder" public="false">

0 commit comments

Comments
 (0)