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

api-platform / core / 10972030337

21 Sep 2024 11:01AM UTC coverage: 7.833% (+0.5%) from 7.372%
10972030337

push

github

dunglas
Merge branch '4.0'

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

4009 existing lines in 283 files now uncovered.

12915 of 164879 relevant lines covered (7.83%)

27.03 hits per line

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

76.77
/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\ProblemExceptionInterface;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\ResourceClassResolverInterface;
24
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
25
use ApiPlatform\State\ApiResource\Error;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\State\Util\RequestAttributesExtractor;
28
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
29
use Negotiation\Negotiator;
30
use Psr\Log\LoggerInterface;
31
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpKernel\EventListener\ErrorListener as SymfonyErrorListener;
34
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
35
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
36

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

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

67
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
68
    {
69
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
299✔
70
        // Because ErrorFormatGuesser is buggy in some cases
71
        $request->setRequestFormat($format);
299✔
72
        $apiOperation = $this->initializeOperation($request);
299✔
73

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

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

83
            return parent::duplicateRequest($exception, $request);
6✔
84
        }
85

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

90
        $dup = parent::duplicateRequest($exception, $request);
293✔
91
        $operation = $this->initializeExceptionOperation($request, $exception, $format, $apiOperation);
293✔
92

93
        if (null === $operation->getProvider()) {
293✔
94
            $operation = $operation->withProvider('api_platform.state.error_provider');
×
95
        }
96

97
        $normalizationContext = ($operation->getNormalizationContext() ?? []) + ($apiOperation?->getNormalizationContext() ?? []);
293✔
98
        if (!($normalizationContext['api_error_resource'] ?? false)) {
293✔
99
            $normalizationContext += ['api_error_resource' => true];
293✔
100
        }
101

102
        if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) {
293✔
103
            $normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString'];
293✔
104
        }
105

106
        $operation = $operation->withNormalizationContext($normalizationContext);
293✔
107

108
        $dup->attributes->set('_api_resource_class', $operation->getClass());
293✔
109
        $dup->attributes->set('_api_previous_operation', $apiOperation);
293✔
110
        $dup->attributes->set('_api_operation', $operation);
293✔
111
        $dup->attributes->set('_api_operation_name', $operation->getName());
293✔
112
        $dup->attributes->set('exception', $exception);
293✔
113
        // These are for swagger
114
        $dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
293✔
115
        $dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
293✔
116
        $dup->attributes->set('_api_original_uri_variables', $request->attributes->get('_api_uri_variables'));
293✔
117
        $dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
293✔
118
        $dup->attributes->set('_api_platform_disable_listeners', true);
293✔
119

120
        return $dup;
293✔
121
    }
122

123
    /**
124
     * @return array<int, array<class-string, int>>
125
     */
126
    private function getOperationExceptionToStatus(Request $request): array
127
    {
128
        $attributes = RequestAttributesExtractor::extractAttributes($request);
12✔
129

130
        if ([] === $attributes) {
12✔
131
            return [];
12✔
132
        }
133

134
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
135
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
136

137
        if (!$operation instanceof HttpOperation) {
×
138
            return [];
×
139
        }
140

141
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
142

143
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
144
            /* @var \ApiPlatform\Metadata\ApiResource; $resourceMetadata */
145
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
146
        }
147

148
        return array_merge(...$exceptionToStatus);
×
149
    }
150

151
    private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?HttpOperation $errorOperation, \Throwable $exception): int
152
    {
153
        $exceptionToStatus = array_merge(
293✔
154
            $this->exceptionToStatus,
293✔
155
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
293✔
156
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
293✔
157
        );
293✔
158

159
        foreach ($exceptionToStatus as $class => $status) {
293✔
160
            if (is_a($exception::class, $class, true)) {
293✔
UNCOV
161
                return $status;
72✔
162
            }
163
        }
164

165
        if ($exception instanceof SymfonyHttpExceptionInterface) {
221✔
166
            return $exception->getStatusCode();
208✔
167
        }
168

169
        if ($exception instanceof ProblemExceptionInterface && $status = $exception->getStatus()) {
13✔
170
            return $status;
×
171
        }
172

173
        if ($exception instanceof HttpExceptionInterface) {
13✔
174
            return $exception->getStatusCode();
×
175
        }
176

177
        if ($exception instanceof RequestExceptionInterface) {
13✔
178
            return 400;
×
179
        }
180

181
        if ($exception instanceof ConstraintViolationListAwareExceptionInterface) {
13✔
182
            return 422;
×
183
        }
184

185
        if ($status = $errorOperation?->getStatus()) {
13✔
186
            return $status;
×
187
        }
188

189
        return 500;
13✔
190
    }
191

192
    private function getFormatOperation(?string $format): string
193
    {
194
        return match ($format) {
203✔
195
            'json' => '_api_errors_problem',
13✔
196
            'jsonproblem' => '_api_errors_problem',
26✔
197
            'jsonld' => '_api_errors_hydra',
153✔
198
            'jsonapi' => '_api_errors_jsonapi',
11✔
199
            'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
×
200
            default => '_api_errors_problem',
203✔
201
        };
203✔
202
    }
203

204
    private function initializeExceptionOperation(?Request $request, \Throwable $exception, string $format, ?HttpOperation $apiOperation): Operation
205
    {
206
        if (!$this->resourceMetadataCollectionFactory) {
293✔
207
            $operation = new ErrorOperation(
×
208
                name: '_api_errors_problem',
×
209
                class: Error::class,
×
210
                outputFormats: ['jsonld' => ['application/problem+json']],
×
211
                normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]
×
212
            );
×
213

214
            return $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
×
215
        }
216

217
        if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
293✔
218
            $resourceCollection = $this->resourceMetadataCollectionFactory->create($exception::class);
90✔
219

220
            $operation = null;
90✔
221
            // TODO: move this to ResourceMetadataCollection?
222
            foreach ($resourceCollection as $resource) {
90✔
223
                foreach ($resource->getOperations() as $op) {
90✔
224
                    foreach ($op->getOutputFormats() as $key => $value) {
90✔
225
                        if ($key === $format) {
90✔
226
                            $operation = $op;
90✔
227
                            break 3;
90✔
228
                        }
229
                    }
230
                }
231
            }
232

233
            // No operation found for the requested format, we take the first available
234
            $operation ??= $resourceCollection->getOperation();
90✔
235

236
            if ($exception instanceof ProblemExceptionInterface && $operation instanceof HttpOperation) {
90✔
237
                return $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
90✔
238
            }
239

240
            return $operation;
×
241
        }
242

243
        // Create a generic, rfc7807 compatible error according to the wanted format
244
        $operation = $this->resourceMetadataCollectionFactory->create(Error::class)->getOperation($this->getFormatOperation($format));
203✔
245
        // status code may be overridden by the exceptionToStatus option
246
        $statusCode = 500;
203✔
247
        if ($operation instanceof HttpOperation) {
203✔
248
            $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
203✔
249
            $operation = $operation->withStatus($statusCode);
203✔
250
        }
251

252
        return $operation;
203✔
253
    }
254
}
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