Skip to content

Commit

Permalink
Improve the Exceptions Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Dec 28, 2019
1 parent 6cc7fb3 commit 6b0c652
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions app/Platform/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function createErrorResponse(HttpException $e, Request $request)

$e = FlattenException::create($e, $status);

if ($request->ajax() || $request->wantsJson()) {
if ($this->isAjaxRequest($request)) {
return Response::json($e->toArray(), $status, $e->getHeaders());
}

Expand All @@ -125,6 +125,17 @@ protected function createErrorResponse(HttpException $e, Request $request)
return Response::make($view->render(), $status, $e->getHeaders());
}

/**
* Returns true if the given Request instance is AJAX.
*
* @param \Nova\Http\Request $request
* @return bool
*/
protected function isAjaxRequest(Request $request)
{
return ($request->ajax() || $request->wantsJson());
}

/**
* Convert the given exception into a Response instance.
*
Expand Down Expand Up @@ -154,11 +165,13 @@ protected function renderExceptionWithWhoops(Exception $e, Request $request)
{
$whoops = new WhoopsRun();

//
// We will instruct Whoops to not exit after it displays the exception as it
// will otherwise run out before we can do anything else. We just want to
// let the framework go ahead and finish a request on this end instead.
$whoops->allowQuit(false);
$whoops->writeToOutput(false);

if ($request->ajax() || $request->wantsJson()) {
if ($this->isAjaxRequest($request)) {
$handler = new WhoopsJsonResponseHandler();
} else {
$handler = new WhoopsPrettyPageHandler();
Expand Down Expand Up @@ -188,7 +201,7 @@ protected function renderExceptionWithWhoops(Exception $e, Request $request)
*/
protected function unauthenticated(Request $request, AuthenticationException $exception)
{
if ($request->ajax() || $request->wantsJson() || $request->is('api/*')) {
if ($this->isAjaxRequest($request) || $request->is('api/*')) {
return Response::json(array('error' => 'Unauthenticated.'), 401);
}

Expand Down

0 comments on commit 6b0c652

Please sign in to comment.