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

api-platform / core / 6222122298

18 Sep 2023 11:54AM UTC coverage: 37.038% (+0.001%) from 37.037%
6222122298

push

github

web-flow
fix: profiler can not serialize closure (#5828)

9 of 9 new or added lines in 1 file covered. (100.0%)

10124 of 27334 relevant lines covered (37.04%)

19.97 hits per line

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

71.43
/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;
17
use ApiPlatform\ApiResource\Error;
18
use ApiPlatform\Metadata\Error as ErrorOperation;
19
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
20
use ApiPlatform\Metadata\HttpOperation;
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\Util\OperationRequestInitiatorTrait;
26
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
27
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
28
use ApiPlatform\Validator\Exception\ValidationException;
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

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

46
    public function __construct(
47
        object|array|string|null $controller,
48
        LoggerInterface $logger = null,
49
        bool $debug = false,
50
        array $exceptionsMapping = [],
51
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
52
        private readonly array $errorFormats = [],
53
        private readonly array $exceptionToStatus = [],
54
        private readonly ?IdentifiersExtractorInterface $identifiersExtractor = null,
55
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
56
        Negotiator $negotiator = null
57
    ) {
58
        parent::__construct($controller, $logger, $debug, $exceptionsMapping);
81✔
59
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
81✔
60
        $this->negotiator = $negotiator ?? new Negotiator();
81✔
61
    }
62

63
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
64
    {
65
        $dup = parent::duplicateRequest($exception, $request);
9✔
66
        $apiOperation = $this->initializeOperation($request);
9✔
67
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
9✔
68

69
        if ($this->resourceMetadataCollectionFactory) {
9✔
70
            if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
9✔
71
                $resourceCollection = $this->resourceMetadataCollectionFactory->create($exception::class);
3✔
72

73
                $operation = null;
3✔
74
                foreach ($resourceCollection as $resource) {
3✔
75
                    foreach ($resource->getOperations() as $op) {
3✔
76
                        foreach ($op->getOutputFormats() as $key => $value) {
3✔
77
                            if ($key === $format) {
3✔
78
                                $operation = $op;
3✔
79
                                break 3;
3✔
80
                            }
81
                        }
82
                    }
83
                }
84

85
                // No operation found for the requested format, we take the first available
86
                if (!$operation) {
3✔
87
                    $operation = $resourceCollection->getOperation();
×
88
                }
89
                $errorResource = $exception;
3✔
90
                if ($errorResource instanceof ProblemExceptionInterface && $operation instanceof HttpOperation) {
3✔
91
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
3✔
92
                    $operation = $operation->withStatus($statusCode);
3✔
93
                    $errorResource->setStatus($statusCode);
3✔
94
                }
95
            } else {
96
                // Create a generic, rfc7807 compatible error according to the wanted format
97
                $operation = $this->resourceMetadataCollectionFactory->create(Error::class)->getOperation($this->getFormatOperation($format));
6✔
98
                // status code may be overriden by the exceptionToStatus option
99
                $statusCode = 500;
6✔
100
                if ($operation instanceof HttpOperation) {
6✔
101
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
6✔
102
                    $operation = $operation->withStatus($statusCode);
6✔
103
                }
104

105
                $errorResource = Error::createFromException($exception, $statusCode);
7✔
106
            }
107
        } else {
108
            /** @var HttpOperation $operation */
109
            $operation = new ErrorOperation(name: '_api_errors_problem', class: Error::class, outputFormats: ['jsonld' => ['application/ld+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]);
×
110
            $operation = $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
×
111
            $errorResource = Error::createFromException($exception, $operation->getStatus());
×
112
        }
113

114
        if (!$operation->getProvider()) {
9✔
115
            $data = 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource;
9✔
116
            $dup->attributes->set('_api_error_resource', $data);
9✔
117
            $operation = $operation->withExtraProperties(['_api_error_resource' => $data])
9✔
118
                                   ->withProvider([self::class, 'provide']);
9✔
119
        }
120

121
        // For our swagger Ui errors
122
        if ('html' === $format) {
9✔
123
            $operation = $operation->withOutputFormats(['html' => ['text/html']]);
×
124
        }
125

126
        $identifiers = [];
9✔
127
        try {
128
            $identifiers = $this->identifiersExtractor?->getIdentifiersFromItem($errorResource, $operation) ?? [];
9✔
129
        } catch (\Exception $e) {
×
130
        }
131

132
        if ($exception instanceof ValidationException) {
9✔
133
            if (!($apiOperation?->getExtraProperties()['rfc_7807_compliant_errors'] ?? false)) {
×
134
                $operation = $operation->withNormalizationContext([
×
135
                    'groups' => ['legacy_'.$format],
×
136
                    'force_iri_generation' => false,
×
137
                ]);
×
138
            }
139
        }
140

141
        $dup->attributes->set('_api_resource_class', $operation->getClass());
9✔
142
        $dup->attributes->set('_api_previous_operation', $apiOperation);
9✔
143
        $dup->attributes->set('_api_operation', $operation);
9✔
144
        $dup->attributes->set('_api_operation_name', $operation->getName());
9✔
145
        $dup->attributes->remove('exception');
9✔
146
        // These are for swagger
147
        $dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
9✔
148
        $dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
9✔
149
        $dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
9✔
150

151
        foreach ($identifiers as $name => $value) {
9✔
152
            $dup->attributes->set($name, $value);
3✔
153
        }
154

155
        return $dup;
9✔
156
    }
157

158
    private function getOperationExceptionToStatus(Request $request): array
159
    {
160
        $attributes = RequestAttributesExtractor::extractAttributes($request);
9✔
161

162
        if ([] === $attributes) {
9✔
163
            return [];
9✔
164
        }
165

166
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
167
        /** @var HttpOperation $operation */
168
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
169
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
170

171
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
172
            /* @var ApiResource $resourceMetadata */
173
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
174
        }
175

176
        return array_merge(...$exceptionToStatus);
×
177
    }
178

179
    private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?HttpOperation $errorOperation, \Throwable $exception): int
180
    {
181
        $exceptionToStatus = array_merge(
9✔
182
            $this->exceptionToStatus,
9✔
183
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
9✔
184
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
9✔
185
        );
9✔
186

187
        foreach ($exceptionToStatus as $class => $status) {
9✔
188
            if (is_a($exception::class, $class, true)) {
×
189
                return $status;
×
190
            }
191
        }
192

193
        if ($exception instanceof SymfonyHttpExceptionInterface) {
9✔
194
            return $exception->getStatusCode();
×
195
        }
196

197
        if ($exception instanceof RequestExceptionInterface) {
9✔
198
            return 400;
×
199
        }
200

201
        if ($exception instanceof ValidationException) {
9✔
202
            return 422;
×
203
        }
204

205
        if ($status = $errorOperation?->getStatus()) {
9✔
206
            return $status;
9✔
207
        }
208

209
        return 500;
×
210
    }
211

212
    private function getFormatOperation(?string $format): string
213
    {
214
        return match ($format) {
6✔
215
            'json' => '_api_errors_problem',
6✔
216
            'jsonproblem' => '_api_errors_problem',
6✔
217
            'jsonld' => '_api_errors_hydra',
6✔
218
            'jsonapi' => '_api_errors_jsonapi',
6✔
219
            'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
6✔
220
            default => '_api_errors_problem'
6✔
221
        };
6✔
222
    }
223

224
    public static function provide(Operation $operation, array $uriVariables = [], array $context = [])
225
    {
226
        if ($data = ($context['request'] ?? null)?->attributes->get('_api_error_resource')) {
×
227
            return $data;
×
228
        }
229

230
        if ($data = $operation->getExtraProperties()['_api_error_resource'] ?? null) {
×
231
            return $data;
×
232
        }
233

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