22
33namespace Dingo \Api \Exception ;
44
5+ use Dingo \Api \Http \Request ;
56use Exception ;
7+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
68use ReflectionFunction ;
79use Illuminate \Support \Str ;
810use Illuminate \Http \Response ;
911use Dingo \Api \Contract \Debug \ExceptionHandler ;
1012use Dingo \Api \Contract \Debug \MessageBagErrors ;
1113use Illuminate \Validation \ValidationException ;
14+ use Symfony \Component \Console \Output \OutputInterface ;
1215use Symfony \Component \HttpFoundation \Response as BaseResponse ;
1316use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
1417use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
1518use Illuminate \Contracts \Debug \ExceptionHandler as IlluminateExceptionHandler ;
19+ use Throwable ;
1620
1721class Handler implements ExceptionHandler, IlluminateExceptionHandler
1822{
@@ -47,14 +51,14 @@ class Handler implements ExceptionHandler, IlluminateExceptionHandler
4751 /**
4852 * The parent Illuminate exception handler instance.
4953 *
50- * @var \Illuminate\Contracts\Debug\ExceptionHandler
54+ * @var IlluminateExceptionHandler
5155 */
5256 protected $ parentHandler ;
5357
5458 /**
5559 * Create a new exception handler instance.
5660 *
57- * @param \Illuminate\Contracts\Debug\ExceptionHandler $parentHandler
61+ * @param IlluminateExceptionHandler $parentHandler
5862 * @param array $format
5963 * @param bool $debug
6064 *
@@ -70,51 +74,51 @@ public function __construct(IlluminateExceptionHandler $parentHandler, array $fo
7074 /**
7175 * Report or log an exception.
7276 *
73- * @param \Exception $exception
77+ * @param Throwable $exception
7478 *
7579 * @return void
7680 */
77- public function report (Exception $ exception )
81+ public function report (Throwable $ throwable )
7882 {
79- $ this ->parentHandler ->report ($ exception );
83+ $ this ->parentHandler ->report ($ throwable );
8084 }
8185
8286 /**
8387 * Determine if the exception should be reported.
8488 *
85- * @param \Exception $e
89+ * @param Throwable $e
8690 *
8791 * @return bool
8892 */
89- public function shouldReport (Exception $ e )
93+ public function shouldReport (Throwable $ e )
9094 {
9195 return true ;
9296 }
9397
9498 /**
9599 * Render an exception into an HTTP response.
96100 *
97- * @param \Dingo\Api\Http\Request $request
98- * @param \Exception $exception
99- *
100- * @throws \Exception
101+ * @param Request $request
102+ * @param Throwable $exception
101103 *
102104 * @return mixed
105+ *@throws Exception
106+ *
103107 */
104- public function render ($ request , Exception $ exception )
108+ public function render ($ request , Throwable $ exception )
105109 {
106110 return $ this ->handle ($ exception );
107111 }
108112
109113 /**
110114 * Render an exception to the console.
111115 *
112- * @param \Symfony\Component\Console\Output\ OutputInterface $output
113- * @param \Exception $exception
116+ * @param OutputInterface $output
117+ * @param Throwable $exception
114118 *
115119 * @return mixed
116120 */
117- public function renderForConsole ($ output , Exception $ exception )
121+ public function renderForConsole ($ output , Throwable $ exception )
118122 {
119123 return $ this ->parentHandler ->renderForConsole ($ output , $ exception );
120124 }
@@ -136,14 +140,14 @@ public function register(callable $callback)
136140 /**
137141 * Handle an exception if it has an existing handler.
138142 *
139- * @param \ Exception $exception
143+ * @param Throwable| Exception $exception
140144 *
141- * @return \Illuminate\Http\ Response
145+ * @return Response
142146 */
143- public function handle (Exception $ exception )
147+ public function handle ($ exception )
144148 {
145149 // Convert Eloquent's 500 ModelNotFoundException into a 404 NotFoundHttpException
146- if ($ exception instanceof \ Illuminate \ Database \ Eloquent \ ModelNotFoundException) {
150+ if ($ exception instanceof ModelNotFoundException) {
147151 $ exception = new NotFoundHttpException ($ exception ->getMessage (), $ exception );
148152 }
149153
@@ -167,13 +171,13 @@ public function handle(Exception $exception)
167171 /**
168172 * Handle a generic error response if there is no handler available.
169173 *
170- * @param \Exception $exception
174+ * @param Throwable $exception
171175 *
172- * @throws \Exception
176+ * @return Response
177+ *@throws Throwable
173178 *
174- * @return \Illuminate\Http\Response
175179 */
176- protected function genericResponse (Exception $ exception )
180+ protected function genericResponse (Throwable $ exception )
177181 {
178182 $ replacements = $ this ->prepareReplacements ($ exception );
179183
@@ -193,11 +197,11 @@ protected function genericResponse(Exception $exception)
193197 /**
194198 * Get the status code from the exception.
195199 *
196- * @param \Exception $exception
200+ * @param Throwable $exception
197201 *
198202 * @return int
199203 */
200- protected function getStatusCode (Exception $ exception )
204+ protected function getStatusCode (Throwable $ exception )
201205 {
202206 if ($ exception instanceof ValidationException) {
203207 return $ exception ->status ;
@@ -209,23 +213,23 @@ protected function getStatusCode(Exception $exception)
209213 /**
210214 * Get the headers from the exception.
211215 *
212- * @param \Exception $exception
216+ * @param Throwable $exception
213217 *
214218 * @return array
215219 */
216- protected function getHeaders (Exception $ exception )
220+ protected function getHeaders (Throwable $ exception )
217221 {
218222 return $ exception instanceof HttpExceptionInterface ? $ exception ->getHeaders () : [];
219223 }
220224
221225 /**
222226 * Prepare the replacements array by gathering the keys and values.
223227 *
224- * @param \Exception $exception
228+ * @param Throwable $exception
225229 *
226230 * @return array
227231 */
228- protected function prepareReplacements (Exception $ exception )
232+ protected function prepareReplacements (Throwable $ exception )
229233 {
230234 $ statusCode = $ this ->getStatusCode ($ exception );
231235
@@ -322,7 +326,7 @@ protected function newResponseArray()
322326 /**
323327 * Get the exception status code.
324328 *
325- * @param \ Exception $exception
329+ * @param Exception $exception
326330 * @param int $defaultStatusCode
327331 *
328332 * @return int
0 commit comments