Created
August 23, 2019 14:07
-
-
Save d8vjork/bfadd8edee54026fe14f97a4cdb06faa to your computer and use it in GitHub Desktop.
Put this in your Laravel's Handler.php to format all the exceptions to JSON:API format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Exception; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
use Illuminate\Http\Exceptions\HttpResponseException; | |
use Illuminate\Auth\AuthenticationException; | |
use Illuminate\Validation\ValidationException; | |
use Illuminate\Auth\Access\AuthorizationException; | |
use Illuminate\Support\Facades\Response; | |
class Handler extends ExceptionHandler | |
{ | |
/** | |
* A list of the exception types that are not reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
// | |
]; | |
/** | |
* A list of the inputs that are never flashed for validation exceptions. | |
* | |
* @var array | |
*/ | |
protected $dontFlash = [ | |
'password', | |
'password_hash', | |
'password_confirmation', | |
'auth_key', | |
]; | |
/** | |
* Report or log an exception. | |
* | |
* @param \Exception $exception | |
* @return void | |
*/ | |
public function report(Exception $exception) | |
{ | |
parent::report($exception); | |
} | |
/** | |
* Render an exception into an HTTP response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $exception | |
* @return \Illuminate\Http\Response | |
*/ | |
public function render($request, Exception $exception) | |
{ | |
if ($request->expectsJson()) { | |
return $this->handleApiException($request, $exception); | |
} | |
if ($exception instanceof AuthorizationException) { | |
return $this->handleAuthorization(); | |
} | |
return parent::render($request, $exception); | |
} | |
private function handleAuthorization() | |
{ | |
return Response::redirectGuest('identify') | |
->withErrors([ | |
'login' => __(\Auth::check() ? 'errors.401' : 'auth.unauthorised') | |
]); | |
} | |
private function handleApiException($request, Exception $exception) | |
{ | |
$exception = $this->prepareException($exception); | |
if ($exception instanceof HttpResponseException) { | |
$exception = $exception->getResponse(); | |
} | |
if ($exception instanceof AuthenticationException) { | |
$exception = $this->unauthenticated($request, $exception); | |
} | |
if ($exception instanceof ValidationException) { | |
$exception = $this->convertValidationExceptionToResponse($exception, $request); | |
} | |
return $this->customApiResponse($exception); | |
} | |
private function customApiResponse($exception) | |
{ | |
$response = []; | |
$response['status'] = (method_exists($exception, 'getStatusCode')) | |
? $exception->getStatusCode() | |
: 500; | |
if (! method_exists($exception, 'getMessage')) { | |
if ($response['status'] !== 422) { | |
$response['message'] = __('errors.' . $response['status']); | |
} else { | |
$response['message'] = $exception->original['message']; | |
$response['errors'] = $exception->original['errors']; | |
} | |
} else { | |
$response['message'] = $exception->getMessage(); | |
} | |
if (config('app.debug') && $response['status'] === 500) { | |
// TODO: Trace as concatenated string on message? | |
$response['trace'] = $exception->getTrace(); | |
$response['code'] = $exception->getCode(); | |
} | |
return response()->json($response, $response['status']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment