Last active
October 26, 2018 10:26
-
-
Save harini-ua/384cf1f5d7886f7f1a1af6e8d2e9e7ca to your computer and use it in GitHub Desktop.
Handle REST API errors thrown from Controller or Exception
This file contains 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 Attend\Exceptions; | |
use Exception; | |
use Attend\Traits\RestTrait; | |
use Attend\Traits\RestExceptionHandlerTrait; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | |
class Handler extends ExceptionHandler | |
{ | |
use RestTrait, RestExceptionHandlerTrait; | |
/** | |
* A list of the exception types that should not be reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
AuthorizationException::class, | |
HttpException::class, | |
ModelNotFoundException::class, | |
ValidationException::class, | |
]; | |
/** | |
* Report or log an exception. | |
* | |
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. | |
* | |
* @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) | |
{ | |
$retval = parent::render($request, $exception); | |
if ($exception instanceof NotFoundHttpException || $this->isApiCall($request)) { | |
$retval = $this->getJsonResponseForException($request, $exception); | |
} | |
return $retval; | |
} | |
} |
This file contains 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\Traits; | |
use Exception; | |
use Illuminate\Http\Request; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
trait RestExceptionHandlerTrait | |
{ | |
/** | |
* Creates a new JSON response based on exception type. | |
* | |
* @param Request $request | |
* @param Exception $e | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function getJsonResponseForException(Request $request, Exception $e) | |
{ | |
switch(true) { | |
case $this->isModelNotFoundException($e): | |
$retval = $this->modelNotFound(); | |
break; | |
default: | |
$retval = $this->badRequest(); | |
} | |
return $retval; | |
} | |
/** | |
* Returns json response for generic bad request. | |
* | |
* @param string $message | |
* @param int $statusCode | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function badRequest($message='Bad request', $statusCode=400) | |
{ | |
return $this->jsonResponse(['error' => $message], $statusCode); | |
} | |
/** | |
* Returns json response for Eloquent model not found exception. | |
* | |
* @param string $message | |
* @param int $statusCode | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function modelNotFound($message='Record not found', $statusCode=404) | |
{ | |
return $this->jsonResponse(['error' => $message], $statusCode); | |
} | |
/** | |
* Returns json response. | |
* | |
* @param array|null $payload | |
* @param int $statusCode | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function jsonResponse(array $payload=null, $statusCode=404) | |
{ | |
$payload = $payload ?: []; | |
return response()->json($payload, $statusCode); | |
} | |
/** | |
* Determines if the given exception is an Eloquent model not found. | |
* | |
* @param Exception $e | |
* @return bool | |
*/ | |
protected function isModelNotFoundException(Exception $e) | |
{ | |
return $e instanceof ModelNotFoundException; | |
} | |
} |
This file contains 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\Traits; | |
use Illuminate\Http\Request; | |
trait RestTrait | |
{ | |
/** | |
* Determines if request is an api call. | |
* | |
* If the request URI contains '/api/v'. | |
* | |
* @param Request $request | |
* @return bool | |
*/ | |
protected function isApiCall(Request $request) | |
{ | |
return strpos($request->getUri(), '/api/v') !== false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment