Skip to content

Commit 6bbd6c2

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

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
2525
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
2626
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
27+
use ApiPlatform\Metadata\Exception\RuntimeException;
2728
use ApiPlatform\Metadata\HeaderParameterInterface;
2829
use ApiPlatform\Metadata\HttpOperation;
2930
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -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,33 @@ 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) {
896+
} catch (\TypeError) {
897897
}
898898

899899
try {
900900
$errorOperation = $this->resourceMetadataFactory->create($error)->getOperation();
901901
if (!is_a($errorOperation, Error::class)) {
902-
$this->logger?->warning(\sprintf('The error class %s is not an ErrorResource', $error));
903-
continue;
902+
throw new RuntimeException(\sprintf('The error class %s is not an ErrorResource', $error));
904903
}
905-
/* @var Error $errorOperation */
906-
$status ??= $errorOperation->getStatus();
907-
$description ??= $errorOperation->getDescription();
908904
} catch (ResourceClassNotFoundException|OperationNotFoundException) {
909-
$this->logger?->warning(\sprintf('The error class %s is not an ErrorResource', $error));
910-
continue;
905+
$errorOperation = null;
911906
}
907+
$status ??= $errorOperation?->getStatus();
908+
$description ??= $errorOperation?->getDescription();
912909

913910
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;
911+
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));
919912
}
920913

921914
$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)