• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.21
/src/Symfony/EventListener/ErrorListener.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
18
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
26
use ApiPlatform\State\ApiResource\Error;
27
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
28
use ApiPlatform\State\Util\RequestAttributesExtractor;
29
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
30
use Negotiation\Negotiator;
31
use Psr\Log\LoggerInterface;
32
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpKernel\EventListener\ErrorListener as SymfonyErrorListener;
35
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
36
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
37

38
/**
39
 * This error listener extends the Symfony one in order to add
40
 * the `_api_operation` attribute when the request is duplicated.
41
 * It will later be used to retrieve the exceptionToStatus from the operation ({@see ApiPlatform\Action\ExceptionAction}).
42
 *
43
 * @internal since API Platform 3.2
44
 */
45
final class ErrorListener extends SymfonyErrorListener
46
{
47
    use ContentNegotiationTrait;
48
    use OperationRequestInitiatorTrait;
49

50
    public function __construct(
51
        object|array|string|null $controller,
52
        ?LoggerInterface $logger = null,
53
        bool $debug = false,
54
        array $exceptionsMapping = [],
55
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
56
        private readonly array $errorFormats = [],
57
        private readonly array $exceptionToStatus = [],
58
        /** @phpstan-ignore-next-line we're not using this anymore but keeping for bc layer */
59
        private readonly ?IdentifiersExtractorInterface $identifiersExtractor = null,
60
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
61
        ?Negotiator $negotiator = null,
62
    ) {
UNCOV
63
        parent::__construct($controller, $logger, $debug, $exceptionsMapping);
946✔
UNCOV
64
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
946✔
UNCOV
65
        $this->negotiator = $negotiator ?? new Negotiator();
946✔
66
    }
67

68
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
69
    {
UNCOV
70
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
102✔
71
        // Reset the request format as it may be that the original request format negotiation won't have the same result
72
        // when an error occurs
UNCOV
73
        $request->setRequestFormat(null);
102✔
UNCOV
74
        $apiOperation = $this->initializeOperation($request);
102✔
75

76
        // TODO: add configuration flag to:
77
        //   - always use symfony error handler (skips this listener)
78
        //   - use symfony error handler if it's not an api error, ie apiOperation is null
79
        //   - use api platform to handle errors (the default behavior we handle firewall errors for example but they're out of our scope)
80

81
        // Let the error handler take this we don't handle HTML nor non-api platform requests
UNCOV
82
        if (false === ($apiOperation?->getExtraProperties()['_api_error_handler'] ?? true) || 'html' === $format) {
102✔
UNCOV
83
            $this->controller = 'error_controller';
2✔
84

UNCOV
85
            return parent::duplicateRequest($exception, $request);
2✔
86
        }
87

UNCOV
88
        if ($this->debug) {
100✔
UNCOV
89
            $this->logger?->error('An exception occured, transforming to an Error resource.', ['exception' => $exception, 'operation' => $apiOperation]);
100✔
90
        }
91

UNCOV
92
        $dup = parent::duplicateRequest($exception, $request);
100✔
UNCOV
93
        $operation = $this->initializeExceptionOperation($request, $exception, $format, $apiOperation);
100✔
94

UNCOV
95
        if (null === $operation->getProvider()) {
100✔
UNCOV
96
            $operation = $operation->withProvider('api_platform.state.error_provider');
1✔
97
        }
98

UNCOV
99
        $normalizationContext = $operation->getNormalizationContext() ?? [];
100✔
UNCOV
100
        if (!($normalizationContext['api_error_resource'] ?? false)) {
100✔
UNCOV
101
            $normalizationContext += ['api_error_resource' => true];
100✔
102
        }
103

UNCOV
104
        if (isset($normalizationContext['item_uri_template'])) {
100✔
105
            unset($normalizationContext['item_uri_template']);
×
106
        }
107

UNCOV
108
        if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) {
100✔
UNCOV
109
            $normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString'];
1✔
110
        }
111

UNCOV
112
        $operation = $operation->withNormalizationContext($normalizationContext);
100✔
113

UNCOV
114
        $dup->attributes->set('_api_resource_class', $operation->getClass());
100✔
UNCOV
115
        $dup->attributes->set('_api_previous_operation', $apiOperation);
100✔
UNCOV
116
        $dup->attributes->set('_api_operation', $operation);
100✔
UNCOV
117
        $dup->attributes->set('_api_operation_name', $operation->getName());
100✔
UNCOV
118
        $dup->attributes->set('exception', $exception);
100✔
119
        // These are for swagger
UNCOV
120
        $dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
100✔
UNCOV
121
        $dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
100✔
UNCOV
122
        $dup->attributes->set('_api_original_uri_variables', $request->attributes->get('_api_uri_variables'));
100✔
UNCOV
123
        $dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
100✔
UNCOV
124
        $dup->attributes->set('_api_platform_disable_listeners', true);
100✔
125

UNCOV
126
        return $dup;
100✔
127
    }
128

129
    /**
130
     * @return array<int, array<class-string, int>>
131
     */
132
    private function getOperationExceptionToStatus(Request $request): array
133
    {
UNCOV
134
        $attributes = RequestAttributesExtractor::extractAttributes($request);
3✔
135

UNCOV
136
        if ([] === $attributes) {
3✔
UNCOV
137
            return [];
3✔
138
        }
139

140
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
141
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
142

143
        if (!$operation instanceof HttpOperation) {
×
144
            return [];
×
145
        }
146

147
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
148

149
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
150
            /* @var \ApiPlatform\Metadata\ApiResource; $resourceMetadata */
151
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
152
        }
153

154
        return array_merge(...$exceptionToStatus);
×
155
    }
156

157
    private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?HttpOperation $errorOperation, \Throwable $exception): int
158
    {
UNCOV
159
        $exceptionToStatus = array_merge(
100✔
UNCOV
160
            $this->exceptionToStatus,
100✔
UNCOV
161
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
100✔
UNCOV
162
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
100✔
UNCOV
163
        );
100✔
164

UNCOV
165
        foreach ($exceptionToStatus as $class => $status) {
100✔
UNCOV
166
            if (is_a($exception::class, $class, true)) {
100✔
167
                return $status;
23✔
168
            }
169
        }
170

UNCOV
171
        if ($exception instanceof SymfonyHttpExceptionInterface) {
77✔
UNCOV
172
            return $exception->getStatusCode();
72✔
173
        }
174

UNCOV
175
        if ($exception instanceof ProblemExceptionInterface && $status = $exception->getStatus()) {
5✔
UNCOV
176
            return $status;
2✔
177
        }
178

UNCOV
179
        if ($exception instanceof HttpExceptionInterface) {
3✔
180
            return $exception->getStatusCode();
×
181
        }
182

UNCOV
183
        if ($exception instanceof RequestExceptionInterface || $exception instanceof InvalidUriVariableException) {
3✔
UNCOV
184
            return 400;
1✔
185
        }
186

187
        if ($exception instanceof ConstraintViolationListAwareExceptionInterface) {
2✔
188
            return 422;
×
189
        }
190

191
        if ($status = $errorOperation?->getStatus()) {
2✔
192
            return $status;
×
193
        }
194

195
        return 500;
2✔
196
    }
197

198
    private function getFormatOperation(?string $format): string
199
    {
UNCOV
200
        return match ($format) {
65✔
UNCOV
201
            'json' => '_api_errors_problem',
4✔
202
            'jsonproblem' => '_api_errors_problem',
7✔
UNCOV
203
            'jsonld' => '_api_errors_hydra',
52✔
204
            'jsonapi' => '_api_errors_jsonapi',
2✔
205
            'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
×
UNCOV
206
            default => '_api_errors_problem',
65✔
UNCOV
207
        };
65✔
208
    }
209

210
    private function initializeExceptionOperation(?Request $request, \Throwable $exception, string $format, ?HttpOperation $apiOperation): Operation
211
    {
UNCOV
212
        if (!$this->resourceMetadataCollectionFactory) {
100✔
213
            $operation = new ErrorOperation(
×
214
                name: '_api_errors_problem',
×
215
                class: Error::class,
×
216
                outputFormats: ['jsonld' => ['application/problem+json']],
×
217
                normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]
×
218
            );
×
219

220
            return $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
×
221
        }
222

UNCOV
223
        if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
100✔
UNCOV
224
            $resourceCollection = $this->resourceMetadataCollectionFactory->create($exception::class);
35✔
225

UNCOV
226
            $operation = null;
35✔
227
            // TODO: move this to ResourceMetadataCollection?
UNCOV
228
            foreach ($resourceCollection as $resource) {
35✔
UNCOV
229
                foreach ($resource->getOperations() as $op) {
35✔
UNCOV
230
                    foreach ($op->getOutputFormats() as $key => $value) {
35✔
UNCOV
231
                        if ($key === $format) {
35✔
UNCOV
232
                            $operation = $op;
35✔
UNCOV
233
                            break 3;
35✔
234
                        }
235
                    }
236
                }
237
            }
238

239
            // No operation found for the requested format, we take the first available
UNCOV
240
            $operation ??= $resourceCollection->getOperation();
35✔
241

UNCOV
242
            if ($exception instanceof ProblemExceptionInterface && $operation instanceof HttpOperation) {
35✔
UNCOV
243
                return $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
35✔
244
            }
245

246
            return $operation;
×
247
        }
248

249
        // Create a generic, rfc7807 compatible error according to the wanted format
UNCOV
250
        $operation = $this->resourceMetadataCollectionFactory->create(Error::class)->getOperation($this->getFormatOperation($format));
65✔
251
        // status code may be overridden by the exceptionToStatus option
UNCOV
252
        $statusCode = 500;
65✔
UNCOV
253
        if ($operation instanceof HttpOperation) {
65✔
UNCOV
254
            $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
65✔
UNCOV
255
            $operation = $operation->withStatus($statusCode);
65✔
256
        }
257

UNCOV
258
        return $operation;
65✔
259
    }
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc