From 1ec2412dbe26823baaed6976233a31ac0a97da24 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Thu, 19 Dec 2019 00:39:10 +0200 Subject: [PATCH] Improve the Exceptions Handling --- app/Platform/Exceptions/Handler.php | 42 +++++++++++++++-------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/Platform/Exceptions/Handler.php b/app/Platform/Exceptions/Handler.php index 692b0466..04f1ab55 100644 --- a/app/Platform/Exceptions/Handler.php +++ b/app/Platform/Exceptions/Handler.php @@ -76,21 +76,13 @@ public function render(Request $request, Exception $e) * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e + * @param \Nova\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e, Request $request) { - $status = $e->getStatusCode(); - - if ($request->ajax() || $request->wantsJson()) { - $e = FlattenException::create($e, $status); - - return Response::json($e->toArray(), $status, $e->getHeaders()); - } - - // If exists a View for this HTTP error. - else if (View::exists("Errors/{$status}")) { - return $this->createErrorResponse($status, $e); + if (! is_null($response = $this->createResponse($e, $request))) { + return $response; } return parent::renderHttpException($e, $request); @@ -100,18 +92,30 @@ protected function renderHttpException(HttpException $e, Request $request) * Convert the given exception into a Response instance which contains an error page. * * @param int $status - * @param \Exception $exception + * @param \Symfony\Component\HttpKernel\Exception\HttpException $e + * @param \Nova\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ - protected function createErrorResponse($status, Exception $e) + protected function createErrorResponse(HttpException $e, Request $request) { - $exception = FlattenException::create($e, $status); + $status = $e->getStatusCode(); + + $e = FlattenException::create($e, $status); + + if ($request->ajax() || $request->wantsJson()) { + return Response::json($e->toArray(), $status, $e->getHeaders()); + } + + // + else if (! View::exists("Errors/{$status}")) { + return; + } $view = View::make('Layouts/Default') ->shares('title', "Error {$status}") - ->nest('content', "Errors/{$status}", compact('exception')); + ->nest('content', "Errors/{$status}", array('exception' => $e)); - return Response::make($view->render(), $status, $exception->getHeaders()); + return Response::make($view->render(), $status, $e->getHeaders()); } /** @@ -123,11 +127,9 @@ protected function createErrorResponse($status, Exception $e) protected function convertExceptionToResponse(Exception $e, Request $request) { if (! Config::get('app.debug', false)) { - if ($request->ajax() || $request->wantsJson()) { - return Response::json('Internal Server Error', 500); - } + $exception = new HttpException(500, 'Internal Server Error'); - return $this->createErrorResponse(500, new Exception('Internal Server Error')); + return $this->createErrorResponse($exception, $request); } return parent::convertExceptionToResponse($e, $request);