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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

93.75
/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
    }
70✔
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) {
70✔
40
            throw new \RuntimeException('Not an HTTP request');
×
41
        }
42

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

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

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

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

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

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

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

UNCOV
81
        return $error;
65✔
82
    }
83

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