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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

73.27
/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
    ) {
63
        parent::__construct($controller, $logger, $debug, $exceptionsMapping);
510✔
64
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
510✔
65
        $this->negotiator = $negotiator ?? new Negotiator();
510✔
66
    }
67

68
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
69
    {
70
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
72✔
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
73
        $request->setRequestFormat(null);
72✔
74
        $apiOperation = $this->initializeOperation($request);
72✔
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
82
        if (false === ($apiOperation?->getExtraProperties()['_api_error_handler'] ?? true) || 'html' === $format) {
72✔
83
            $this->controller = 'error_controller';
2✔
84

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

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

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

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

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

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

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

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

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

126
        return $dup;
70✔
127
    }
128

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

136
        if ([] === $attributes) {
2✔
137
            return [];
2✔
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
    {
159
        $exceptionToStatus = array_merge(
70✔
160
            $this->exceptionToStatus,
70✔
161
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
70✔
162
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
70✔
163
        );
70✔
164

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

171
        if ($exception instanceof SymfonyHttpExceptionInterface) {
70✔
172
            return $exception->getStatusCode();
64✔
173
        }
174

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

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

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

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

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

195
        return 500;
×
196
    }
197

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

210
    private function initializeExceptionOperation(?Request $request, \Throwable $exception, string $format, ?HttpOperation $apiOperation): Operation
211
    {
212
        if (!$this->resourceMetadataCollectionFactory) {
70✔
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

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

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

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

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

246
            return $operation;
×
247
        }
248

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

258
        return $operation;
26✔
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