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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

7493 of 12362 relevant lines covered (60.61%)

67.56 hits per line

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

66.67
/src/Action/ExceptionAction.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\Action;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Util\ErrorFormatGuesser;
20
use ApiPlatform\Util\OperationRequestInitiatorTrait;
21
use ApiPlatform\Util\RequestAttributesExtractor;
22
use Symfony\Component\ErrorHandler\Exception\FlattenException;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Serializer\SerializerInterface;
26

27
/**
28
 * Renders a normalized exception for a given {@see FlattenException}.
29
 *
30
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ExceptionAction
34
{
35
    use OperationRequestInitiatorTrait;
36

37
    /**
38
     * @param array $errorFormats      A list of enabled error formats
39
     * @param array $exceptionToStatus A list of exceptions mapped to their HTTP status code
40
     */
41
    public function __construct(private readonly SerializerInterface $serializer, private readonly array $errorFormats, private readonly array $exceptionToStatus = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
42
    {
43
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
45✔
44
    }
45

46
    /**
47
     * Converts an exception to a JSON response.
48
     */
49
    public function __invoke(FlattenException $exception, Request $request): Response
50
    {
51
        $operation = $this->initializeOperation($request);
45✔
52
        $exceptionClass = $exception->getClass();
45✔
53
        $statusCode = $exception->getStatusCode();
45✔
54

55
        $exceptionToStatus = array_merge(
45✔
56
            $this->exceptionToStatus,
45✔
57
            $operation ? $operation->getExceptionToStatus() ?? [] : $this->getOperationExceptionToStatus($request)
45✔
58
        );
45✔
59

60
        foreach ($exceptionToStatus as $class => $status) {
45✔
61
            if (is_a($exceptionClass, $class, true)) {
45✔
62
                $statusCode = $status;
24✔
63

64
                break;
24✔
65
            }
66
        }
67

68
        $headers = $exception->getHeaders();
45✔
69
        $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats);
45✔
70
        $headers['Content-Type'] = sprintf('%s; charset=utf-8', $format['value'][0]);
45✔
71
        $headers['X-Content-Type-Options'] = 'nosniff';
45✔
72
        $headers['X-Frame-Options'] = 'deny';
45✔
73

74
        return new Response($this->serializer->serialize($exception, $format['key'], ['statusCode' => $statusCode]), $statusCode, $headers);
45✔
75
    }
76

77
    private function getOperationExceptionToStatus(Request $request): array
78
    {
79
        $attributes = RequestAttributesExtractor::extractAttributes($request);
×
80

81
        if ([] === $attributes) {
×
82
            return [];
×
83
        }
84

85
        $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($attributes['resource_class']);
×
86
        /** @var HttpOperation $operation */
87
        $operation = $resourceMetadataCollection->getOperation($attributes['operation_name'] ?? null);
×
88
        $exceptionToStatus = [$operation->getExceptionToStatus() ?: []];
×
89

90
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
91
            /* @var ApiResource $resourceMetadata */
92
            $exceptionToStatus[] = $resourceMetadata->getExceptionToStatus() ?: [];
×
93
        }
94

95
        return array_merge(...$exceptionToStatus);
×
96
    }
97
}
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