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

api-platform / core / 6867212600

14 Nov 2023 05:15PM UTC coverage: 37.474% (-0.01%) from 37.484%
6867212600

push

github

soyuka
chore: phpstan and deprecations

3 of 3 new or added lines in 2 files covered. (100.0%)

90 existing lines in 7 files now uncovered.

10323 of 27547 relevant lines covered (37.47%)

13.81 hits per line

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

76.85
/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\Api\IdentifiersExtractorInterface as LegacyIdentifiersExtractorInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\ApiResource\Error;
19
use ApiPlatform\Metadata\Error as ErrorOperation;
20
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
28
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
29
use ApiPlatform\Validator\Exception\ValidationException;
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

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
    private static mixed $error;
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
        private readonly null|IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface $identifiersExtractor = null,
59
        private readonly null|ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver = null,
60
        Negotiator $negotiator = null,
61
        private readonly bool $problemCompliantErrors = true
62
    ) {
63
        parent::__construct($controller, $logger, $debug, $exceptionsMapping);
62✔
64
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
62✔
65
        $this->negotiator = $negotiator ?? new Negotiator();
62✔
66
    }
67

68
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
69
    {
70
        $apiOperation = $this->initializeOperation($request);
12✔
71
        if (false === $this->problemCompliantErrors) {
12✔
72
            // TODO: deprecate in API Platform 3.3
73
            $this->controller = 'api_platform.action.exception';
2✔
74
            $dup = parent::duplicateRequest($exception, $request);
2✔
75
            $dup->attributes->set('_api_operation', $apiOperation);
2✔
76

77
            return $dup;
2✔
78
        }
79

80
        if ($this->debug) {
10✔
81
            $this->logger?->error('An exception occured, transforming to an Error resource.', ['exception' => $exception, 'operation' => $apiOperation]);
8✔
82
        }
83

84
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
10✔
85

86
        // Let the error handler take this we don't handle HTML
87
        if ('html' === $format) {
10✔
UNCOV
88
            $this->controller = 'error_controller';
×
UNCOV
89
            $dup = parent::duplicateRequest($exception, $request);
×
90

UNCOV
91
            return $dup;
×
92
        }
93

94
        $dup = parent::duplicateRequest($exception, $request);
10✔
95
        if ($this->resourceMetadataCollectionFactory) {
10✔
96
            if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
10✔
97
                $resourceCollection = $this->resourceMetadataCollectionFactory->create($exception::class);
2✔
98

99
                $operation = null;
2✔
100
                foreach ($resourceCollection as $resource) {
2✔
101
                    foreach ($resource->getOperations() as $op) {
2✔
102
                        foreach ($op->getOutputFormats() as $key => $value) {
2✔
103
                            if ($key === $format) {
2✔
104
                                $operation = $op;
2✔
105
                                break 3;
2✔
106
                            }
107
                        }
108
                    }
109
                }
110

111
                // No operation found for the requested format, we take the first available
112
                if (!$operation) {
2✔
UNCOV
113
                    $operation = $resourceCollection->getOperation();
×
114
                }
115
                $errorResource = $exception;
2✔
116
                if ($errorResource instanceof ProblemExceptionInterface && $operation instanceof HttpOperation) {
2✔
117
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
2✔
118
                    $operation = $operation->withStatus($statusCode);
2✔
119
                    $errorResource->setStatus($statusCode);
2✔
120
                }
121
            } else {
122
                // Create a generic, rfc7807 compatible error according to the wanted format
123
                $operation = $this->resourceMetadataCollectionFactory->create(Error::class)->getOperation($this->getFormatOperation($format));
8✔
124
                // status code may be overriden by the exceptionToStatus option
125
                $statusCode = 500;
8✔
126
                if ($operation instanceof HttpOperation) {
8✔
127
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
8✔
128
                    $operation = $operation->withStatus($statusCode);
8✔
129
                }
130

131
                $errorResource = Error::createFromException($exception, $statusCode);
8✔
132
            }
133
        } else {
134
            /** @var HttpOperation $operation */
UNCOV
135
            $operation = new ErrorOperation(name: '_api_errors_problem', class: Error::class, outputFormats: ['jsonld' => ['application/problem+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]);
×
UNCOV
136
            $operation = $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
×
137
            $errorResource = Error::createFromException($exception, $operation->getStatus());
×
138
        }
139

140
        if (!$operation->getProvider()) {
10✔
141
            static::$error = 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource;
10✔
142
            $operation = $operation->withProvider([self::class, 'provide']);
10✔
143
        }
144

145
        /** @var HttpOperation $operation */
146
        if (!$this->debug && $operation->getStatus() >= 500 && $errorResource instanceof Error) {
10✔
147
            $errorResource->setDetail('Internal Server Error');
2✔
148
        }
149

150
        $identifiers = [];
10✔
151
        try {
152
            $identifiers = $this->identifiersExtractor?->getIdentifiersFromItem($errorResource, $operation) ?? [];
10✔
153
        } catch (\Exception $e) {
2✔
154
        }
155

156
        if ($exception instanceof ValidationException && !($apiOperation?->getExtraProperties()['rfc_7807_compliant_errors'] ?? false)) {
10✔
UNCOV
157
            $operation = $operation->withNormalizationContext([
×
UNCOV
158
                'groups' => ['legacy_'.$format],
×
UNCOV
159
                'force_iri_generation' => false,
×
UNCOV
160
            ]);
×
161
        }
162

163
        if ($apiOperation && 'jsonld' === $format && !($apiOperation->getExtraProperties()['rfc_7807_compliant_errors'] ?? false)) {
10✔
UNCOV
164
            $operation = $operation->withOutputFormats(['jsonld' => ['application/ld+json']])
×
UNCOV
165
                                   ->withLinks([])
×
UNCOV
166
                                   ->withExtraProperties(['rfc_7807_compliant_errors' => false] + $operation->getExtraProperties());
×
167
        }
168

169
        $dup->attributes->set('_api_resource_class', $operation->getClass());
10✔
170
        $dup->attributes->set('_api_previous_operation', $apiOperation);
10✔
171
        $dup->attributes->set('_api_operation', $operation);
10✔
172
        $dup->attributes->set('_api_operation_name', $operation->getName());
10✔
173
        $dup->attributes->set('exception', $exception);
10✔
174
        // These are for swagger
175
        $dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
10✔
176
        $dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
10✔
177
        $dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
10✔
178

179
        foreach ($identifiers as $name => $value) {
10✔
180
            $dup->attributes->set($name, $value);
4✔
181
        }
182

183
        return $dup;
10✔
184
    }
185

186
    private function getOperationExceptionToStatus(Request $request): array
187
    {
188
        $attributes = RequestAttributesExtractor::extractAttributes($request);
8✔
189

190
        if ([] === $attributes) {
8✔
191
            return [];
8✔
192
        }
193

UNCOV
194
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
195
        /** @var HttpOperation $operation */
UNCOV
196
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
UNCOV
197
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
198

UNCOV
199
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
200
            /* @var ApiResource $resourceMetadata */
UNCOV
201
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
202
        }
203

UNCOV
204
        return array_merge(...$exceptionToStatus);
×
205
    }
206

207
    private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?HttpOperation $errorOperation, \Throwable $exception): int
208
    {
209
        $exceptionToStatus = array_merge(
10✔
210
            $this->exceptionToStatus,
10✔
211
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
10✔
212
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
10✔
213
        );
10✔
214

215
        foreach ($exceptionToStatus as $class => $status) {
10✔
216
            if (is_a($exception::class, $class, true)) {
2✔
UNCOV
217
                return $status;
×
218
            }
219
        }
220

221
        if ($exception instanceof SymfonyHttpExceptionInterface) {
10✔
UNCOV
222
            return $exception->getStatusCode();
×
223
        }
224

225
        if ($exception instanceof RequestExceptionInterface) {
10✔
UNCOV
226
            return 400;
×
227
        }
228

229
        if ($exception instanceof ValidationException) {
10✔
UNCOV
230
            return 422;
×
231
        }
232

233
        if ($status = $errorOperation?->getStatus()) {
10✔
234
            return $status;
6✔
235
        }
236

237
        return 500;
4✔
238
    }
239

240
    private function getFormatOperation(?string $format): string
241
    {
242
        return match ($format) {
8✔
243
            'json' => '_api_errors_problem',
8✔
244
            'jsonproblem' => '_api_errors_problem',
8✔
245
            'jsonld' => '_api_errors_hydra',
8✔
246
            'jsonapi' => '_api_errors_jsonapi',
8✔
247
            'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
8✔
248
            default => '_api_errors_problem'
8✔
249
        };
8✔
250
    }
251

252
    public static function provide(): mixed
253
    {
254
        if ($data = static::$error) {
4✔
255
            return $data;
4✔
256
        }
257

UNCOV
258
        throw new \LogicException(sprintf('We could not find the thrown exception in the %s.', self::class));
×
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