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\ExceptionHandling\Exception\ProblemException; |
17
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\ProblemExceptionInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Transforms a {@see Throwable} into a {@see ProblemException} implementation. |
23
|
|
|
* Adding additional information to the {@see ProblemException} from the exception map. |
24
|
|
|
* |
25
|
|
|
* @author Niels Nijens <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ThrowableToProblemExceptionTransformer implements ThrowableToProblemExceptionTransformerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array<string, array> |
31
|
|
|
*/ |
32
|
|
|
private $exceptionMap; |
33
|
|
|
|
34
|
|
|
public function __construct(array $exceptionMap) |
35
|
|
|
{ |
36
|
|
|
$this->exceptionMap = $exceptionMap; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function transform(Throwable $throwable, ?string $instanceUri): ProblemExceptionInterface |
40
|
|
|
{ |
41
|
|
|
$class = get_class($throwable); |
42
|
|
|
$exceptionData = $this->exceptionMap[$class] ?? []; |
43
|
|
|
$exceptionData['instance_uri'] = $instanceUri; |
44
|
|
|
|
45
|
|
|
if ($throwable instanceof HttpExceptionInterface) { |
46
|
|
|
$throwable = ProblemException::fromHttpException($throwable, $throwable->getStatusCode()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($throwable instanceof ProblemExceptionInterface === false) { |
50
|
|
|
$throwable = ProblemException::fromThrowable($throwable); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->addAdditionalProblemInformation($throwable, $exceptionData); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array{type_uri: string, title: string, add_instance_uri: bool, instance_uri: string, status_code: int} $exceptionData |
58
|
|
|
*/ |
59
|
|
|
private function addAdditionalProblemInformation( |
60
|
|
|
ProblemExceptionInterface $exception, |
61
|
|
|
array $exceptionData |
62
|
|
|
): ProblemExceptionInterface { |
63
|
|
|
if (isset($exceptionData['type_uri'])) { |
64
|
|
|
$exception = $exception->withTypeUri($exceptionData['type_uri']); |
65
|
|
|
} |
66
|
|
|
if (isset($exceptionData['title'])) { |
67
|
|
|
$exception = $exception->withTitle($exceptionData['title']); |
68
|
|
|
} |
69
|
|
|
if (($exceptionData['add_instance_uri'] ?? false) && isset($exceptionData['instance_uri'])) { |
70
|
|
|
$exception = $exception->withInstanceUri($exceptionData['instance_uri']); |
71
|
|
|
} |
72
|
|
|
if (isset($exceptionData['status_code'])) { |
73
|
|
|
$exception = $exception->withStatusCode($exceptionData['status_code']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $exception; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|