1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Exceptions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException; |
7
|
|
|
use Napp\Core\Api\Exceptions\Renderer\DebugRenderer; |
8
|
|
|
use Napp\Core\Api\Exceptions\Renderer\Renderer; |
9
|
|
|
use Napp\Core\Api\Exceptions\Renderer\RendererInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class NappApiHandler. |
13
|
|
|
*/ |
14
|
|
|
class NappApiHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RendererInterface |
18
|
|
|
*/ |
19
|
|
|
protected $displayer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \Throwable $e |
23
|
|
|
* |
24
|
|
|
* @throws \ReflectionException |
25
|
|
|
*/ |
26
|
|
|
public function __construct(\Throwable $e) |
27
|
|
|
{ |
28
|
|
|
if (true === config('app.debug')) { |
29
|
|
|
$this->displayer = new DebugRenderer(); |
30
|
|
|
} else { |
31
|
|
|
$this->displayer = new Renderer(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->displayer->setException($e); |
35
|
|
|
$this->displayer->setResponseCode($this->getResponseCode($e)); |
36
|
|
|
$this->displayer->setStatusCode($this->getStatusCode($e)); |
37
|
|
|
$this->displayer->setStatusMessage($this->getStatusMessage($e)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return JsonResponse |
42
|
|
|
*/ |
43
|
|
|
public function render(): JsonResponse |
44
|
|
|
{ |
45
|
|
|
return $this->displayer->render(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \Throwable $e |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
protected function getResponseCode(\Throwable $e): ?int |
54
|
|
|
{ |
55
|
|
|
if (true === $e instanceof NappException) { |
56
|
|
|
/* @var NappException $e */ |
57
|
|
|
return $e->getResponseCode(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (true === \method_exists($e, 'getStatusCode')) { |
61
|
|
|
return $e->getStatusCode(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (\property_exists($e, 'status')) { |
65
|
|
|
return $e->status; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
switch (last(explode('\\', \get_class($e)))) { |
69
|
|
|
case 'ModelNotFoundException': |
70
|
|
|
return 404; |
71
|
|
|
default: |
72
|
|
|
return 500; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \Throwable $e |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
protected function getStatusCode(\Throwable $e): ?int |
82
|
|
|
{ |
83
|
|
|
if (true === $e instanceof NappException) { |
84
|
|
|
/* @var NappException $e */ |
85
|
|
|
return $e->getStatusCode(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($e->getCode() && \is_int($e->getCode())) { |
89
|
|
|
return $e->getCode(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
switch ($this->getResponseCode($e)) { |
93
|
|
|
case 400: |
94
|
|
|
return 220; |
95
|
|
|
case 401: |
96
|
|
|
return 135; |
97
|
|
|
case 403: |
98
|
|
|
return 64; |
99
|
|
|
case 404: |
100
|
|
|
return 34; |
101
|
|
|
case 405: |
102
|
|
|
return 34; |
103
|
|
|
default: |
104
|
|
|
return 500; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \Throwable $e |
110
|
|
|
* |
111
|
|
|
* @throws \ReflectionException |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getStatusMessage(\Throwable $e): string |
116
|
|
|
{ |
117
|
|
|
if (true === $e instanceof NappException) { |
118
|
|
|
/* @var NappException $e */ |
119
|
|
|
return $e->getStatusMessage(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($e->getMessage()) { |
123
|
|
|
return $e->getMessage(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return (new \ReflectionClass($e))->getShortName(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|