1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Exceptions\Renderer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DebugRenderer. |
10
|
|
|
*/ |
11
|
|
|
class DebugRenderer implements RendererInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Exception |
15
|
|
|
*/ |
16
|
|
|
protected $exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $statusCode; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $statusMessage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $responseCode; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return JsonResponse |
35
|
|
|
*/ |
36
|
|
|
public function render(): JsonResponse |
37
|
|
|
{ |
38
|
|
|
if ($this->exception instanceof \JsonSerializable) { |
39
|
|
|
return response()->json(array_merge_recursive( |
40
|
|
|
$this->exception->jsonSerialize(), |
41
|
|
|
[ |
42
|
|
|
'error' => [ |
43
|
|
|
'type' => \get_class($this->exception), |
44
|
|
|
'file' => $this->exception->getFile(), |
45
|
|
|
'line' => $this->exception->getLine(), |
46
|
|
|
'trace' => $this->formatTrace($this->exception->getTrace()), |
47
|
|
|
], |
48
|
|
|
]), |
49
|
|
|
$this->responseCode); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return response()->json( |
53
|
|
|
[ |
54
|
|
|
'error' => [ |
55
|
|
|
'code' => $this->statusCode, |
56
|
|
|
'message' => $this->statusMessage, |
57
|
|
|
'type' => \get_class($this->exception), |
58
|
|
|
'file' => $this->exception->getFile(), |
59
|
|
|
'line' => $this->exception->getLine(), |
60
|
|
|
'trace' => $this->formatTrace($this->exception->getTrace()), |
61
|
|
|
], ], |
62
|
|
|
$this->responseCode |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \Throwable $e |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function setException(\Throwable $e) |
72
|
|
|
{ |
73
|
|
|
$this->exception = $e; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $responseCode |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function setResponseCode($responseCode) |
82
|
|
|
{ |
83
|
|
|
$this->responseCode = $responseCode; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $statusCode |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function setStatusCode($statusCode) |
92
|
|
|
{ |
93
|
|
|
$this->statusCode = $statusCode; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $statusMessage |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function setStatusMessage($statusMessage) |
102
|
|
|
{ |
103
|
|
|
$this->statusMessage = $statusMessage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove the args property from the trace array objects. |
108
|
|
|
* |
109
|
|
|
* @param array $trace |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
protected function formatTrace(array $trace) |
114
|
|
|
{ |
115
|
|
|
foreach ($trace as &$t) { |
116
|
|
|
$t = Arr::except($t, ['args']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $trace; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.