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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

78.35
/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\Metadata\Error as ErrorOperation;
19
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
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\ApiResource\Error;
27
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
28
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
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
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 null|IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface $identifiersExtractor = null,
60
        private readonly null|ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver = null,
61
        Negotiator $negotiator = null,
62
        private readonly bool $problemCompliantErrors = true,
63
    ) {
64
        parent::__construct($controller, $logger, $debug, $exceptionsMapping);
2,496✔
65
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
2,496✔
66
        $this->negotiator = $negotiator ?? new Negotiator();
2,496✔
67
    }
68

69
    protected function duplicateRequest(\Throwable $exception, Request $request): Request
70
    {
71
        $format = $this->getRequestFormat($request, $this->errorFormats, false);
300✔
72
        // Because ErrorFormatGuesser is buggy in some cases
73
        $request->setRequestFormat($format);
300✔
74
        $apiOperation = $this->initializeOperation($request);
300✔
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 ('html' === $format) {
300✔
83
            $this->controller = 'error_controller';
3✔
84

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

88
        $legacy = $apiOperation ? ($apiOperation->getExtraProperties()['rfc_7807_compliant_errors'] ?? false) : $this->problemCompliantErrors;
297✔
89

90
        if (!$this->problemCompliantErrors || !$legacy) {
297✔
91
            // TODO: deprecate in API Platform 3.3
92
            $this->controller = 'api_platform.action.exception';
108✔
93
            $dup = parent::duplicateRequest($exception, $request);
108✔
94
            $dup->attributes->set('_api_operation', $apiOperation);
108✔
95
            $dup->attributes->set('_api_exception_action', true);
108✔
96

97
            return $dup;
108✔
98
        }
99

100
        if ($this->debug) {
189✔
101
            $this->logger?->error('An exception occured, transforming to an Error resource.', ['exception' => $exception, 'operation' => $apiOperation]);
189✔
102
        }
103

104
        $dup = parent::duplicateRequest($exception, $request);
189✔
105
        if ($this->resourceMetadataCollectionFactory) {
189✔
106
            if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
189✔
107
                $resourceCollection = $this->resourceMetadataCollectionFactory->create($exception::class);
27✔
108

109
                $operation = null;
27✔
110
                foreach ($resourceCollection as $resource) {
27✔
111
                    foreach ($resource->getOperations() as $op) {
27✔
112
                        foreach ($op->getOutputFormats() as $key => $value) {
27✔
113
                            if ($key === $format) {
27✔
114
                                $operation = $op;
27✔
115
                                break 3;
27✔
116
                            }
117
                        }
118
                    }
119
                }
120

121
                // No operation found for the requested format, we take the first available
122
                if (!$operation) {
27✔
123
                    $operation = $resourceCollection->getOperation();
×
124
                }
125
                if ($exception instanceof ProblemExceptionInterface && $operation instanceof HttpOperation) {
27✔
126
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
27✔
127
                    $operation = $operation->withStatus($statusCode);
27✔
128
                }
129
            } else {
130
                // Create a generic, rfc7807 compatible error according to the wanted format
131
                $operation = $this->resourceMetadataCollectionFactory->create(Error::class)->getOperation($this->getFormatOperation($format));
162✔
132
                // status code may be overriden by the exceptionToStatus option
133
                $statusCode = 500;
162✔
134
                if ($operation instanceof HttpOperation) {
162✔
135
                    $statusCode = $this->getStatusCode($apiOperation, $request, $operation, $exception);
162✔
136
                    $operation = $operation->withStatus($statusCode);
171✔
137
                }
138
            }
139
        } else {
140
            /** @var HttpOperation $operation */
141
            $operation = new ErrorOperation(name: '_api_errors_problem', class: Error::class, outputFormats: ['jsonld' => ['application/problem+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]);
×
142
            $operation = $operation->withStatus($this->getStatusCode($apiOperation, $request, $operation, $exception));
×
143
        }
144

145
        if (null === $operation->getProvider()) {
189✔
UNCOV
146
            $operation = $operation->withProvider('api_platform.state.error_provider');
×
147
        }
148

149
        $normalizationContext = $operation->getNormalizationContext() ?? [];
189✔
150
        if (!($normalizationContext['_api_error_resource'] ?? false)) {
189✔
151
            $normalizationContext += ['api_error_resource' => true];
189✔
152
        }
153

154
        if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) {
189✔
155
            $normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString'];
189✔
156
        }
157

158
        $operation = $operation->withNormalizationContext($normalizationContext);
189✔
159

160
        $dup->attributes->set('_api_resource_class', $operation->getClass());
189✔
161
        $dup->attributes->set('_api_previous_operation', $apiOperation);
189✔
162
        $dup->attributes->set('_api_operation', $operation);
189✔
163
        $dup->attributes->set('_api_operation_name', $operation->getName());
189✔
164
        $dup->attributes->set('exception', $exception);
189✔
165
        // These are for swagger
166
        $dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
189✔
167
        $dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
189✔
168
        $dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
189✔
169
        $dup->attributes->set('_api_platform_disable_listeners', true);
189✔
170

171
        return $dup;
189✔
172
    }
173

174
    private function getOperationExceptionToStatus(Request $request): array
175
    {
176
        $attributes = RequestAttributesExtractor::extractAttributes($request);
3✔
177

178
        if ([] === $attributes) {
3✔
179
            return [];
3✔
180
        }
181

182
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
183
        /** @var HttpOperation $operation */
184
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
185
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
186

187
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
188
            /* @var \ApiPlatform\Metadata\ApiResource; $resourceMetadata */
189
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
190
        }
191

192
        return array_merge(...$exceptionToStatus);
×
193
    }
194

195
    private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?HttpOperation $errorOperation, \Throwable $exception): int
196
    {
197
        $exceptionToStatus = array_merge(
189✔
198
            $this->exceptionToStatus,
189✔
199
            $apiOperation ? $apiOperation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request),
189✔
200
            $errorOperation ? $errorOperation->getExceptionToStatus() ?? [] : []
189✔
201
        );
189✔
202

203
        foreach ($exceptionToStatus as $class => $status) {
189✔
204
            if (is_a($exception::class, $class, true)) {
189✔
205
                return $status;
96✔
206
            }
207
        }
208

209
        if ($exception instanceof SymfonyHttpExceptionInterface) {
93✔
210
            return $exception->getStatusCode();
93✔
211
        }
212

UNCOV
213
        if ($exception instanceof ProblemExceptionInterface && $status = $exception->getStatus()) {
×
UNCOV
214
            return $status;
×
215
        }
216

UNCOV
217
        if ($exception instanceof HttpExceptionInterface) {
×
218
            return $exception->getStatusCode();
×
219
        }
220

UNCOV
221
        if ($exception instanceof RequestExceptionInterface) {
×
222
            return 400;
×
223
        }
224

UNCOV
225
        if ($exception instanceof ValidationException) {
×
226
            return 422;
×
227
        }
228

UNCOV
229
        if ($status = $errorOperation?->getStatus()) {
×
UNCOV
230
            return $status;
×
231
        }
232

233
        return 500;
×
234
    }
235

236
    private function getFormatOperation(?string $format): string
237
    {
238
        return match ($format) {
162✔
239
            'json' => '_api_errors_problem',
162✔
240
            'jsonproblem' => '_api_errors_problem',
162✔
241
            'jsonld' => '_api_errors_hydra',
162✔
242
            'jsonapi' => '_api_errors_jsonapi',
162✔
243
            'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
162✔
244
            default => '_api_errors_problem'
162✔
245
        };
162✔
246
    }
247
}
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

© 2026 Coveralls, Inc