| @@ 25-58 (lines=34) @@ | ||
| 22 | * |
|
| 23 | * @author Baptiste Meyer <[email protected]> |
|
| 24 | */ |
|
| 25 | final class ExceptionAction |
|
| 26 | { |
|
| 27 | private $normalizer; |
|
| 28 | ||
| 29 | public function __construct(NormalizerInterface $normalizer) |
|
| 30 | { |
|
| 31 | $this->normalizer = $normalizer; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Converts a {@see \Symfony\Component\Debug\Exception\FlattenException} |
|
| 36 | * to a {@see \ApiPlatform\Core\JsonLd\Response}. |
|
| 37 | * |
|
| 38 | * @param FlattenException $exception |
|
| 39 | * |
|
| 40 | * @return Response |
|
| 41 | */ |
|
| 42 | public function __invoke(FlattenException $exception) |
|
| 43 | { |
|
| 44 | $exceptionClass = $exception->getClass(); |
|
| 45 | if ( |
|
| 46 | is_a($exceptionClass, ExceptionInterface::class, true) || |
|
| 47 | is_a($exceptionClass, InvalidArgumentException::class, true) |
|
| 48 | ) { |
|
| 49 | $exception->setStatusCode(Response::HTTP_BAD_REQUEST); |
|
| 50 | } |
|
| 51 | ||
| 52 | return new Response( |
|
| 53 | $this->normalizer->normalize($exception, 'hydra-error'), |
|
| 54 | $exception->getStatusCode(), |
|
| 55 | $exception->getHeaders() |
|
| 56 | ); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| @@ 25-58 (lines=34) @@ | ||
| 22 | * |
|
| 23 | * @author Baptiste Meyer <[email protected]> |
|
| 24 | */ |
|
| 25 | final class ExceptionAction |
|
| 26 | { |
|
| 27 | private $normalizer; |
|
| 28 | ||
| 29 | public function __construct(NormalizerInterface $normalizer) |
|
| 30 | { |
|
| 31 | $this->normalizer = $normalizer; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Converts a {@see \Symfony\Component\Debug\Exception\FlattenException} |
|
| 36 | * to a {@see \ApiPlatform\Core\JsonLd\Response}. |
|
| 37 | * |
|
| 38 | * @param FlattenException $exception |
|
| 39 | * |
|
| 40 | * @return Response |
|
| 41 | */ |
|
| 42 | public function __invoke(FlattenException $exception) |
|
| 43 | { |
|
| 44 | $exceptionClass = $exception->getClass(); |
|
| 45 | if ( |
|
| 46 | is_a($exceptionClass, ExceptionInterface::class, true) || |
|
| 47 | is_a($exceptionClass, InvalidArgumentException::class, true) |
|
| 48 | ) { |
|
| 49 | $exception->setStatusCode(Response::HTTP_BAD_REQUEST); |
|
| 50 | } |
|
| 51 | ||
| 52 | return new Response( |
|
| 53 | $this->normalizer->normalize($exception, 'swagger-error'), |
|
| 54 | $exception->getStatusCode(), |
|
| 55 | $exception->getHeaders() |
|
| 56 | ); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||