1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the OpenapiBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Niels Nijens <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\ExceptionHandling; |
15
|
|
|
|
16
|
|
|
use Nijens\OpenapiBundle\Exception\HttpExceptionInterface; |
17
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\InvalidRequestBodyProblemException; |
18
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\ProblemExceptionInterface; |
19
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\Violation; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Transforms a deprecated exception implementing the {@see HttpExceptionInterface} |
25
|
|
|
* to {@see InvalidRequestBodyProblemException}. |
26
|
|
|
* |
27
|
|
|
* @deprecated since 1.3, to be removed in 2.0. |
28
|
|
|
*/ |
29
|
|
|
final class DeprecatedExceptionToProblemExceptionTransformer implements ThrowableToProblemExceptionTransformerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ThrowableToProblemExceptionTransformerInterface |
33
|
|
|
*/ |
34
|
|
|
private $baseThrowableToProblemExceptionTransformer; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
ThrowableToProblemExceptionTransformerInterface $baseThrowableToProblemExceptionTransformer |
38
|
|
|
) { |
39
|
|
|
$this->baseThrowableToProblemExceptionTransformer = $baseThrowableToProblemExceptionTransformer; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function transform(Throwable $throwable, ?string $instanceUri): ProblemExceptionInterface |
43
|
|
|
{ |
44
|
|
|
if ($throwable instanceof HttpExceptionInterface) { |
45
|
|
|
$exception = $this->createInvalidRequestBodyProblemException($throwable); |
46
|
|
|
|
47
|
|
|
$violations = array_map( |
48
|
|
|
function (array $error): Violation { |
49
|
|
|
$error['constraint'] = $error['constraint'] ?? 'valid_json'; |
50
|
|
|
|
51
|
|
|
return Violation::fromArray($error); |
52
|
|
|
}, |
53
|
|
|
$throwable->getErrors() |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$throwable = $exception->withViolations($violations); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->baseThrowableToProblemExceptionTransformer->transform($throwable, $instanceUri); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function createInvalidRequestBodyProblemException(Throwable $throwable): InvalidRequestBodyProblemException |
63
|
|
|
{ |
64
|
|
|
if ($throwable instanceof SymfonyHttpExceptionInterface) { |
65
|
|
|
return InvalidRequestBodyProblemException::fromHttpException($throwable); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return InvalidRequestBodyProblemException::fromThrowable($throwable); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|