Files
microservice-orders-api/app/Exceptions/Handler.php
T
Behzad Babaei 851454db11 initial commit
2020-03-28 17:02:54 +04:30

96 lines
2.8 KiB
PHP

<?php
namespace App\Exceptions;
use App\Traits\ApiResponse;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
class Handler extends ExceptionHandler
{
use ApiResponse;
/**
* 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)
{
if ($exception instanceof HttpException) {
$errorCode = $exception->getStatusCode();
$errorMessage = Response::$statusTexts[$errorCode];
return $this->errorResponse($errorMessage, $errorCode);
}
if ($exception instanceof ModelNotFoundException) {
$model = strtolower(class_basename($exception->getModel()));
return $this->errorResponse(
"Does not exist any instance of {$model} with a given id",
Response::HTTP_NOT_FOUND);
}
if ($exception instanceof AuthorizationException) {
return $this->errorResponse(
$exception->getMessage(),
Response::HTTP_FORBIDDEN);
}
if ($exception instanceof AuthenticationException) {
return $this->errorResponse(
$exception->getMessage(),
Response::HTTP_UNAUTHORIZED);
}
if ($exception instanceof ValidationException) {
$errors = $exception->validator->errors()->getMessages();
return $this->errorResponse(
$errors,
Response::HTTP_UNPROCESSABLE_ENTITY);
}
if(env('APP_DEBUG',false)){
return parent::render($request, $exception);
}
return $this->errorResponse(
"Unexpected error. Please try later or contact the support team",
Response::HTTP_INTERNAL_SERVER_ERROR);
}
}