Skip to content

Commit

Permalink
Improve the Exceptions Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Dec 18, 2019
1 parent aef6e1d commit 1ec2412
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions app/Platform/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}

/**
Expand All @@ -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);
Expand Down

0 comments on commit 1ec2412

Please sign in to comment.