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