Completed
Push — master ( 906aaa...a12ac4 )
by Han Hui
26s queued 13s
created

src/Action/ExceptionAction.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Action;
15
16
use ApiPlatform\Core\Util\ErrorFormatGuesser;
17
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
18
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
0 ignored issues
show
The type Symfony\Component\ErrorR...eption\FlattenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Serializer\SerializerInterface;
22
23
/**
24
 * Renders a normalized exception for a given {@see FlattenException} or {@see LegacyFlattenException}.
25
 *
26
 * @author Baptiste Meyer <[email protected]>
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
final class ExceptionAction
30
{
31
    private $serializer;
32
    private $errorFormats;
33
    private $exceptionToStatus;
34
35
    /**
36
     * @param array $errorFormats      A list of enabled error formats
37
     * @param array $exceptionToStatus A list of exceptions mapped to their HTTP status code
38
     */
39
    public function __construct(SerializerInterface $serializer, array $errorFormats, array $exceptionToStatus = [])
40
    {
41
        $this->serializer = $serializer;
42
        $this->errorFormats = $errorFormats;
43
        $this->exceptionToStatus = $exceptionToStatus;
44
    }
45
46
    /**
47
     * Converts an exception to a JSON response.
48
     *
49
     * @param FlattenException|LegacyFlattenException $exception
50
     */
51
    public function __invoke($exception, Request $request): Response
52
    {
53
        $exceptionClass = $exception->getClass();
54
        $statusCode = $exception->getStatusCode();
55
56
        foreach ($this->exceptionToStatus as $class => $status) {
57
            if (is_a($exceptionClass, $class, true)) {
58
                $statusCode = $status;
59
60
                break;
61
            }
62
        }
63
64
        $headers = $exception->getHeaders();
65
        $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats);
66
        $headers['Content-Type'] = sprintf('%s; charset=utf-8', $format['value'][0]);
67
        $headers['X-Content-Type-Options'] = 'nosniff';
68
        $headers['X-Frame-Options'] = 'deny';
69
70
        return new Response($this->serializer->serialize($exception, $format['key'], ['statusCode' => $statusCode]), $statusCode, $headers);
71
    }
72
}
73