1 | <?php |
||
12 | trait RestException |
||
13 | { |
||
14 | /** |
||
15 | * Datas of response |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $datas = []; |
||
20 | |||
21 | /** |
||
22 | * In development mode return trace of exception |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $trace = []; |
||
27 | |||
28 | /** |
||
29 | * Creates a new JSON response based on exception type. |
||
30 | * |
||
31 | * @author Alireza Josheghani <[email protected]> |
||
32 | * @since 11 Nov 2016 |
||
33 | * @param Exception $exception |
||
34 | * @return \Anetwork\Respond\Facades\Respond |
||
35 | */ |
||
36 | protected function getJsonResponseForException(Exception $exception) |
||
37 | { |
||
38 | $message = $exception->getMessage(); |
||
39 | |||
40 | $this->getTrace($exception); |
||
41 | |||
42 | if ($exception instanceof ModelNotFoundException) { |
||
43 | return $this->modelNotFound($message); |
||
44 | } elseif ($exception instanceof NotFoundHttpException) { |
||
45 | return $this->httpNotFound(); |
||
46 | } elseif ($exception instanceof BadRequestHttpException) { |
||
47 | return $this->badRequest($message); |
||
48 | } |
||
49 | |||
50 | return $this->errorException($message); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Returns json response for generic bad request. |
||
55 | * |
||
56 | * @author Alireza Josheghani <[email protected]> |
||
57 | * @since 11 Nov 2016 |
||
58 | * @param string $message |
||
59 | * @return \Anetwork\Respond\Facades\Respond |
||
60 | */ |
||
61 | protected function badRequest($message) |
||
69 | |||
70 | /** |
||
71 | * Returns json response ErrorException |
||
72 | * |
||
73 | * @author Alireza Josheghani <[email protected]> |
||
74 | * @since 11 Nov 2016 |
||
75 | * @param string $message |
||
76 | * @return \Anetwork\Respond\Facades\Respond |
||
77 | */ |
||
78 | protected function errorException($message) |
||
86 | |||
87 | /** |
||
88 | * Returns json response for Eloquent model not found exception. |
||
89 | * |
||
90 | * @author Alireza Josheghani <[email protected]> |
||
91 | * @since 11 Nov 2016 |
||
92 | * @param string $message |
||
93 | * @return \Anetwork\Respond\Facades\Respond |
||
94 | */ |
||
95 | protected function modelNotFound($message) |
||
103 | |||
104 | /** |
||
105 | * Returns json response for Eloquent model not found exception. |
||
106 | * |
||
107 | * @author Alireza Josheghani <[email protected]> |
||
108 | * @since 11 Nov 2016 |
||
109 | * @return \Anetwork\Respond\Facades\Respond |
||
110 | */ |
||
111 | protected function httpNotFound() |
||
119 | |||
120 | /** |
||
121 | * Get Trace in development mode |
||
122 | * |
||
123 | * @author Alireza Josheghani <[email protected]> |
||
124 | * @since 11 Nov 2016 |
||
125 | * @param Exception $exception |
||
126 | * @return void |
||
127 | */ |
||
128 | private function getTrace(Exception $exception) |
||
145 | |||
146 | /** |
||
147 | * Render json exception |
||
148 | * |
||
149 | * @author Alireza Josheghani <[email protected]> |
||
150 | * @since 11 Nov 2016 |
||
151 | * @param Request $request |
||
152 | * @param Exception $exception |
||
153 | * @return \Anetwork\Respond\Facades\Respond |
||
154 | */ |
||
155 | public function renderRestException(Request $request, Exception $exception) |
||
159 | |||
160 | } |
||
161 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.