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 177388c commit aef6e1d
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions app/Platform/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,30 @@ protected function renderHttpException(HttpException $e, Request $request)

// If exists a View for this HTTP error.
else if (View::exists("Errors/{$status}")) {
$view = View::make('Layouts/Default')
->shares('title', "Error {$status}")
->nest('content', "Errors/{$status}", array('exception' => $e));

return Response::make($view->render(), $status, $e->getHeaders());
return $this->createErrorResponse($status, $e);
}

return parent::renderHttpException($e, $request);
}

/**
* Convert the given exception into a Response instance which contains an error page.
*
* @param int $status
* @param \Exception $exception
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function createErrorResponse($status, Exception $e)
{
$exception = FlattenException::create($e, $status);

$view = View::make('Layouts/Default')
->shares('title', "Error {$status}")
->nest('content', "Errors/{$status}", compact('exception'));

return Response::make($view->render(), $status, $exception->getHeaders());
}

/**
* Convert the given exception into a Response instance.
*
Expand All @@ -108,23 +122,15 @@ protected function renderHttpException(HttpException $e, Request $request)
*/
protected function convertExceptionToResponse(Exception $e, Request $request)
{
$debug = Config::get('app.debug');

if (! $debug) {
$e = FlattenException::create($e);

if (! Config::get('app.debug', false)) {
if ($request->ajax() || $request->wantsJson()) {
return Response::json('Internal Server Error', 500);
}

// Not an AJAX request.
else if ( View::exists('Errors/500')) {
return $this->renderHttpException($e, $request);
}
return $this->createErrorResponse(500, new Exception('Internal Server Error'));
}

return parent::convertExceptionToResponse($e, $request);

}

/**
Expand Down

0 comments on commit aef6e1d

Please sign in to comment.