|
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
|
|
|
namespace ApiPlatform\Core\Action; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
15
|
|
|
use ApiPlatform\Core\Util\ErrorFormatGuesser; |
|
16
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\Serializer\Exception\ExceptionInterface; |
|
20
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Renders a normalized exception for a given {@see \Symfony\Component\Debug\Exception\FlattenException}. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Baptiste Meyer <[email protected]> |
|
26
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class ExceptionAction |
|
29
|
|
|
{ |
|
30
|
|
|
const DEFAULT_EXCEPTION_TO_STATUS = [ |
|
31
|
|
|
ExceptionInterface::class => Response::HTTP_BAD_REQUEST, |
|
32
|
|
|
InvalidArgumentException::class => Response::HTTP_BAD_REQUEST, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
private $serializer; |
|
36
|
|
|
private $errorFormats; |
|
37
|
|
|
private $exceptionToStatus; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(SerializerInterface $serializer, array $errorFormats, $exceptionToStatus = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->serializer = $serializer; |
|
42
|
|
|
$this->errorFormats = $errorFormats; |
|
43
|
|
|
$this->exceptionToStatus = array_merge(self::DEFAULT_EXCEPTION_TO_STATUS, $exceptionToStatus); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Converts a an exception to a JSON response. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Exception|FlattenException $exception |
|
50
|
|
|
* @param Request $request |
|
51
|
|
|
* |
|
52
|
|
|
* @return Response |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __invoke($exception, Request $request) : Response |
|
55
|
|
|
{ |
|
56
|
|
|
$exceptionClass = $exception->getClass(); |
|
|
|
|
|
|
57
|
|
|
foreach ($this->exceptionToStatus as $class => $status) { |
|
58
|
|
|
if (is_a($exceptionClass, $class, true)) { |
|
59
|
|
|
$exception->setStatusCode($status); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$headers = $exception->getHeaders(); |
|
|
|
|
|
|
66
|
|
|
$format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats); |
|
67
|
|
|
$headers['Content-Type'] = $format['value'][0]; |
|
68
|
|
|
|
|
69
|
|
|
return new Response($this->serializer->serialize($exception, $format['key']), $exception->getStatusCode(), $headers); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: