1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saikootau\ApiBundle\Resource\Builder; |
6
|
|
|
|
7
|
|
|
use Saikootau\ApiBundle\Exception\ExposableError; |
8
|
|
|
use Saikootau\ApiBundle\Resource\Error; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
10
|
|
|
use Throwable; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionException; |
13
|
|
|
|
14
|
|
|
class ErrorResourceBuilder |
15
|
|
|
{ |
16
|
|
|
private $exposeStack = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trace the whole exception stack. |
20
|
|
|
* |
21
|
|
|
* @return ErrorResourceBuilder |
22
|
|
|
*/ |
23
|
2 |
|
public function exposeStack(): self |
24
|
|
|
{ |
25
|
2 |
|
$this->exposeStack = true; |
26
|
|
|
|
27
|
2 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Only evaluate the topmost exception in stack. |
32
|
|
|
* |
33
|
|
|
* @return ErrorResourceBuilder |
34
|
|
|
*/ |
35
|
9 |
|
public function doNotExposeStack(): self |
36
|
|
|
{ |
37
|
9 |
|
$this->exposeStack = false; |
38
|
|
|
|
39
|
9 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Build an error resource from an exception. |
44
|
|
|
* |
45
|
|
|
* @param Throwable $exception |
46
|
|
|
* |
47
|
|
|
* @throws ReflectionException |
48
|
|
|
* |
49
|
|
|
* @return Error[] |
50
|
|
|
*/ |
51
|
12 |
|
public function build(Throwable $exception): array |
52
|
|
|
{ |
53
|
12 |
|
$errors = []; |
54
|
|
|
|
55
|
12 |
|
if ($this->shouldBeExposed($exception)) { |
56
|
8 |
|
$errors[] = $this->createError($exception); |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
if ($exception->getPrevious()) { |
60
|
3 |
|
$errors = array_merge($errors, $this->build($exception->getPrevious())); |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
return $errors; |
64
|
|
|
} |
65
|
|
|
|
66
|
12 |
|
private function shouldBeExposed(Throwable $exception): bool |
67
|
|
|
{ |
68
|
12 |
|
if ($this->exposeStack) { |
69
|
3 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
return $exception instanceof HttpExceptionInterface |
73
|
9 |
|
|| $exception instanceof ExposableError |
74
|
|
|
; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create an error from an exception. |
79
|
|
|
* |
80
|
|
|
* @param Throwable $exception |
81
|
|
|
* |
82
|
|
|
* @throws ReflectionException |
83
|
|
|
* |
84
|
|
|
* @return Error |
85
|
|
|
*/ |
86
|
8 |
|
private function createError(Throwable $exception): Error |
87
|
|
|
{ |
88
|
8 |
|
return new Error($this->getErrorName($exception), $exception->getMessage(), (string) $exception->getCode()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get name for given exception to show in stack. |
93
|
|
|
* |
94
|
|
|
* @param Throwable $exception |
95
|
|
|
* |
96
|
|
|
* @throws ReflectionException |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
8 |
|
private function getErrorName(Throwable $exception): string |
101
|
|
|
{ |
102
|
8 |
|
if ($exception instanceof ExposableError) { |
103
|
2 |
|
return $exception->getShowName(); |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
$reflection = new ReflectionClass($exception); |
107
|
|
|
|
108
|
7 |
|
return $reflection->getShortName(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|