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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

0 of 2 new or added lines in 2 files covered. (0.0%)

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/State/ErrorProvider.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\State;
15

16
use ApiPlatform\Metadata\ErrorResourceInterface;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\ResourceClassResolverInterface;
21
use ApiPlatform\State\ApiResource\Error;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24

25
/**
26
 * @internal
27
 */
28
final class ErrorProvider implements ProviderInterface
29
{
30
    public function __construct(private readonly bool $debug = false, private ?ResourceClassResolverInterface $resourceClassResolver = null, private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
31
    {
UNCOV
32
    }
×
33

34
    /**
35
     * @param array{status?: int} $uriVariables
36
     */
37
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
38
    {
UNCOV
39
        if (!($request = $context['request'] ?? null) || !$operation instanceof HttpOperation) {
×
40
            throw new \RuntimeException('Not an HTTP request');
×
41
        }
42

UNCOV
43
        if (!($exception = $request->attributes->get('exception'))) {
×
UNCOV
44
            $status = $uriVariables['status'] ?? null;
×
45

46
            // We change the operation to get our normalization context according to the format
UNCOV
47
            if ($this->resourceMetadataCollectionFactory) {
×
UNCOV
48
                $resourceCollection = $this->resourceMetadataCollectionFactory->create($operation->getClass());
×
UNCOV
49
                foreach ($resourceCollection as $resource) {
×
UNCOV
50
                    foreach ($resource->getOperations() as $name => $operation) {
×
UNCOV
51
                        if (isset($operation->getOutputFormats()[$request->getRequestFormat()])) {
×
UNCOV
52
                            $request->attributes->set('_api_operation', $operation);
×
UNCOV
53
                            $request->attributes->set('_api_operation_nme', $name);
×
UNCOV
54
                            break 2;
×
55
                        }
56
                    }
57
                }
58
            }
59

UNCOV
60
            $text = Response::$statusTexts[$status] ?? throw new NotFoundHttpException();
×
61

UNCOV
62
            $cl = $operation->getClass();
×
63

UNCOV
64
            return match ($request->getRequestFormat()) {
×
UNCOV
65
                'html' => $this->renderError((int) $status, $text),
×
UNCOV
66
                default => new $cl("Error $status", $text, (int) $status),
×
UNCOV
67
            };
×
68
        }
69

UNCOV
70
        if ($this->resourceClassResolver?->isResourceClass($exception::class)) {
×
UNCOV
71
            return $exception;
×
72
        }
73

UNCOV
74
        $status = $operation->getStatus() ?? 500;
×
UNCOV
75
        $cl = is_a($operation->getClass(), ErrorResourceInterface::class, true) ? $operation->getClass() : Error::class;
×
UNCOV
76
        $error = $cl::createFromException($exception, $status);
×
UNCOV
77
        if (!$this->debug && $status >= 500 && method_exists($error, 'setDetail')) {
×
78
            $error->setDetail('Internal Server Error');
×
79
        }
80

UNCOV
81
        return $error;
×
82
    }
83

84
    private function renderError(int $status, string $text): Response
85
    {
UNCOV
86
        return new Response(<<<HTML
×
UNCOV
87
<!DOCTYPE html>
×
88
<html>
89
<head>
90
  <meta charset="UTF-8" />
UNCOV
91
  <title>Error $status</title>
×
92
    </head>
UNCOV
93
    <body><h1>Error $status</h1>$text</body>
×
94
</html>
UNCOV
95
HTML);
×
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