From 8a392da8acf5db7086e9070e16b508660ec183d0 Mon Sep 17 00:00:00 2001 From: Quynh Xuan Nguyen Date: Wed, 8 Dec 2021 09:31:03 +0700 Subject: [PATCH 1/5] Refactor ExceptionHandler render method --- .../Foundation/Exceptions/Handler.php | 72 ++++++++++++------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 4a950cc7a43f..83c89a9d7bc6 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -323,12 +323,37 @@ public function render($request, Throwable $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); - } elseif ($e instanceof Responsable) { + } + + if ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($this->mapException($e)); + if ($response = $this->renderViaCallbacks($request, $e)) { + return $response; + } + + return match(true) { + $e instanceof HttpResponseException => $e->getResponse(), + $e instanceof AuthenticationException => $this->unauthenticated($request, $e), + $e instanceof ValidationException => $this->convertValidationExceptionToResponse($e, $request), + default => $this->renderExceptionResponse($request, $e), + }; + } + + /** + * Try to render a response from request and exception via render callbacks. + * + * @param \Illuminate\Http\Request $request + * @param \Throwable $e + * @return mixed + * + * @throws \ReflectionException + */ + protected function renderViaCallbacks($request, Throwable $e) + { foreach ($this->renderCallbacks as $renderCallback) { foreach ($this->firstClosureParameterTypes($renderCallback) as $type) { if (is_a($e, $type)) { @@ -340,18 +365,22 @@ public function render($request, Throwable $e) } } } + } - if ($e instanceof HttpResponseException) { - return $e->getResponse(); - } elseif ($e instanceof AuthenticationException) { - return $this->unauthenticated($request, $e); - } elseif ($e instanceof ValidationException) { - return $this->convertValidationExceptionToResponse($e, $request); - } - + /** + * Render a default exception response if any. + * + * @param \Illuminate\Http\Request $request + * @param \Throwable $e + * @return mixed + * + * @return \Symfony\Component\HttpFoundation\Response + */ + protected function renderExceptionResponse($request, Throwable $e) + { return $this->shouldReturnJson($request, $e) - ? $this->prepareJsonResponse($request, $e) - : $this->prepareResponse($request, $e); + ? $this->prepareJsonResponse($request, $e) + : $this->prepareResponse($request, $e); } /** @@ -379,19 +408,14 @@ protected function mapException(Throwable $e) */ protected function prepareException(Throwable $e) { - if ($e instanceof ModelNotFoundException) { - $e = new NotFoundHttpException($e->getMessage(), $e); - } elseif ($e instanceof AuthorizationException) { - $e = new AccessDeniedHttpException($e->getMessage(), $e); - } elseif ($e instanceof TokenMismatchException) { - $e = new HttpException(419, $e->getMessage(), $e); - } elseif ($e instanceof SuspiciousOperationException) { - $e = new NotFoundHttpException('Bad hostname provided.', $e); - } elseif ($e instanceof RecordsNotFoundException) { - $e = new NotFoundHttpException('Not found.', $e); - } - - return $e; + return match(true) { + $e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), + $e instanceof AuthorizationException => new AccessDeniedHttpException($e->getMessage(), $e), + $e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e), + $e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e), + $e instanceof RecordsNotFoundException => new NotFoundHttpException('Not found.', $e), + default => $e, + }; } /** From a49ca995427c4b7144dca22ef583adda0711527f Mon Sep 17 00:00:00 2001 From: Quynh Xuan Nguyen Date: Wed, 8 Dec 2021 09:32:42 +0700 Subject: [PATCH 2/5] Restore unexpected format change --- src/Illuminate/Foundation/Exceptions/Handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 83c89a9d7bc6..0d522f4954ff 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -379,8 +379,8 @@ protected function renderViaCallbacks($request, Throwable $e) protected function renderExceptionResponse($request, Throwable $e) { return $this->shouldReturnJson($request, $e) - ? $this->prepareJsonResponse($request, $e) - : $this->prepareResponse($request, $e); + ? $this->prepareJsonResponse($request, $e) + : $this->prepareResponse($request, $e); } /** From c2164e7f370507c9f2300f4ce22ce47c4090a48f Mon Sep 17 00:00:00 2001 From: Quynh Xuan Nguyen Date: Wed, 8 Dec 2021 10:06:07 +0700 Subject: [PATCH 3/5] Fix coding style --- src/Illuminate/Foundation/Exceptions/Handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 0d522f4954ff..0605f636decd 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -335,7 +335,7 @@ public function render($request, Throwable $e) return $response; } - return match(true) { + return match (true) { $e instanceof HttpResponseException => $e->getResponse(), $e instanceof AuthenticationException => $this->unauthenticated($request, $e), $e instanceof ValidationException => $this->convertValidationExceptionToResponse($e, $request), @@ -408,7 +408,7 @@ protected function mapException(Throwable $e) */ protected function prepareException(Throwable $e) { - return match(true) { + return match (true) { $e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), $e instanceof AuthorizationException => new AccessDeniedHttpException($e->getMessage(), $e), $e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e), From 903285326354ee811488f04bc39b6aeef19ec6b1 Mon Sep 17 00:00:00 2001 From: Quynh Xuan Nguyen Date: Wed, 8 Dec 2021 10:07:33 +0700 Subject: [PATCH 4/5] Fix coding style --- src/Illuminate/Foundation/Exceptions/Handler.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 0605f636decd..72039e46c973 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -372,8 +372,6 @@ protected function renderViaCallbacks($request, Throwable $e) * * @param \Illuminate\Http\Request $request * @param \Throwable $e - * @return mixed - * * @return \Symfony\Component\HttpFoundation\Response */ protected function renderExceptionResponse($request, Throwable $e) From e029bd0f2b60a9f3676082f5371ff54ee2633409 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 8 Dec 2021 07:01:45 -0600 Subject: [PATCH 5/5] formatting --- .../Foundation/Exceptions/Handler.php | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 72039e46c973..4fc71ec99d4b 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -343,6 +343,41 @@ public function render($request, Throwable $e) }; } + /** + * Prepare exception for rendering. + * + * @param \Throwable $e + * @return \Throwable + */ + protected function prepareException(Throwable $e) + { + return match (true) { + $e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), + $e instanceof AuthorizationException => new AccessDeniedHttpException($e->getMessage(), $e), + $e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e), + $e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e), + $e instanceof RecordsNotFoundException => new NotFoundHttpException('Not found.', $e), + default => $e, + }; + } + + /** + * Map the exception using a registered mapper if possible. + * + * @param \Throwable $e + * @return \Throwable + */ + protected function mapException(Throwable $e) + { + foreach ($this->exceptionMap as $class => $mapper) { + if (is_a($e, $class)) { + return $mapper($e); + } + } + + return $e; + } + /** * Try to render a response from request and exception via render callbacks. * @@ -381,41 +416,6 @@ protected function renderExceptionResponse($request, Throwable $e) : $this->prepareResponse($request, $e); } - /** - * Map the exception using a registered mapper if possible. - * - * @param \Throwable $e - * @return \Throwable - */ - protected function mapException(Throwable $e) - { - foreach ($this->exceptionMap as $class => $mapper) { - if (is_a($e, $class)) { - return $mapper($e); - } - } - - return $e; - } - - /** - * Prepare exception for rendering. - * - * @param \Throwable $e - * @return \Throwable - */ - protected function prepareException(Throwable $e) - { - return match (true) { - $e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), - $e instanceof AuthorizationException => new AccessDeniedHttpException($e->getMessage(), $e), - $e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e), - $e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e), - $e instanceof RecordsNotFoundException => new NotFoundHttpException('Not found.', $e), - default => $e, - }; - } - /** * Convert an authentication exception into a response. *